필터 지우기
필터 지우기

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.

조회 수: 3 (최근 30일)
So far I have:
clc,clear
day=input('Enter date of the month ');
if any(day==[3 10 17 24])
dayName = 'Monday' ;
elseif any(day==[4 11 18 25])
dayName = 'Tuesday' ;
elseif any(day==[5 12 19 26])
dayName = 'Wednesday' ;
elseif any(day==[6 13 20 27])
dayName = 'Thursday' ;
elseif any(day==[7 14 21 28])
dayName = 'Friday' ;
elseif any(day==[1 8 15 22 29])
dayName = 'Saturday';
elseif any(day==[2 9 16 23 30])
dayName = 'Sunday';
end
disp(dayName)
However, I am unable to get multiple values to work at once. Any help would be greatly appreciated!

채택된 답변

Image Analyst
Image Analyst 2014년 11월 21일
Close, but use a switch statement instead of an if. And use sscanf to get the numbers into a numerical array and use a for loop to process each of the numbers in the list:
clc;
clear;
userInput = input('Enter date of the month (separated by spaces) ', 's')
days = sscanf(userInput, '%f')
for k = 1 : length(days)
switch days(k)
case {3, 10, 17, 24}
dayName = 'Monday' ;
case {4, 11, 18, 25}
dayName = 'Tuesday' ;
case {5, 12, 19, 26}
dayName = 'Wednesday' ;
case {6, 13, 20, 27}
dayName = 'Thursday' ;
case {7, 14, 21, 28}
dayName = 'Friday' ;
case {1, 8, 15, 22, 29}
dayName = 'Saturday';
case {2, 9, 16, 23, 30}
dayName = 'Sunday';
otherwise
dayName = 'Unknown';
end
fprintf('The day of the week for #%d is %s.\n', days(k), dayName);
end
  댓글 수: 1
Jarred
Jarred 2014년 11월 21일
This is extremely helpful! Now I will work at arranging it so the solutions are on a single line, seperated by a comma. Thank you!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by