Using MATLAB write a code for the following: Usually, cells are most useful for
ID: 2079855 • Letter: U
Question
Using MATLAB write a code for the following:
Usually, cells are most useful for storing strings, because the length of each string can be unique. Make a 4 times 4 cell where the first column contains the names: 'Kevin', 'Natasha', 'Dak' and 'Usain', the second column contains their last names: 'Hart', 'St. Pier', 'Prescott', 'Bolt', the third column contains their occupations: 'Comedian', 'Singer', 'Quarterback', 'Athlete' and the last column their salaries: $850,000, $650,000, and $1, 500,000, and $2,00,000. Display the cell using disp. Modify the code you obtained in a. such that it displays a sentence of the form "[First Name], [Last Name] is a/an [Occupation] and has a salary of [Salary]". Do this using a "for loop" and display the sentences.Explanation / Answer
matlab Code:
A = {'Kevin' 'Hart' 'Comedian' '$850,000';
'Natasha' 'St. Pier' 'Singer' '$650,000';
'Dak' 'Prescott' 'Quaterback' '$1,500,000';
'Usain' 'Bolt' 'Athlete' '$2,000,000'};
disp(A);
%part b
for i=1:length(A)
X = sprintf('%s, %s is a/an %s and has a salary of %s ',char(A(i,1)),char(A(i,2)),char(A(i,3)),char(A(i,4)));
disp(X);
end
command window output:
'Kevin' 'Hart' 'Comedian' '$850,000'
'Natasha' 'St. Pier' 'Singer' '$650,000'
'Dak' 'Prescott' 'Quaterback' '$1,500,000'
'Usain' 'Bolt' 'Athlete' '$2,000,000'
Kevin, Hart is a/an Comedian and has a salary of $850,000
Natasha, St. Pier is a/an Singer and has a salary of $650,000
Dak, Prescott is a/an Quaterback and has a salary of $1,500,000
Usain, Bolt is a/an Athlete and has a salary of $2,000,000