Problem 1 Functions, Selection Statements, MATLAB Programming a) Write a functio
ID: 2290826 • Letter: P
Question
Problem 1 Functions, Selection Statements, MATLAB Programming a)
Write a function dayName that has one input argument, day, containing the numerical value of a day in the month of November, 2016. Your function should return the name of that day as a string. For example: dayName(3) should return 'Thursday'
b) You are now given a variable named days, a vector that contains the numeric values of days in the month of November, 2016. Write a script that will convert each numeric value in the vector days into a string named daysOfWeek with the day names separated by a comma and a space. For example, if days = [3, 4, 5], daysOfWeek should be 'Thursday, Friday, Saturday'. Notice that there is no separator before the first day or after the last day.
Explanation / Answer
%%% Function
function day = dayName (n)
switch n
case 6
day=string('Sunday');
case 0
day=string('Monday');
case 1
day=string('Tuesday');
case 2
day=string('Wednesday');
case 3
day=string('Thusday');
case 4
day=string('Friday');
case 5
day=string('Saturday');
end
end
OUTPUT:
>> dayName(3)
ans =
"Thusday"
%%%%%%%%%%%%%%
(b)
%%%
dy=[4 0 2];
day= string;
for n=1:length(dy)
day(n)=dayName(dy(n));
end
disp(day)
OUTPUT:
"Friday" "Monday" "Wednesday"
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.