Trying to request user input and then perform if elseif else end function...

조회 수: 2 (최근 30일)
I have a for loop which generates a figure on each loop. Based on what I see in the figure, I want my code to request a user input of either Y or N.
Then, on input of either Y or N, I would like to add a row to an output matrix, which I will ultimately write to a .csv.
The first column in each row will be the filename of the figure, and the second column, either 'yesboat' or 'notboat' depending on whether the input is Y or N.
This code isn't working AT ALL but it's what I have pulled together from the help files... if you have any suggestions to make it work, I would be very grateful.
Also, which is the best way to create an empty output for storing text info. such as this? My filenames are numbers at the moment but I would like them to be stored as text.
%create output array, table with two columns
output=[];
row=1;
prompt='Can you see boat noise? Y/N';
x=input(prompt);
if x=='Y'
%store filename in output with 'boat' in second column
output(row,1) = filename; %e.g.5103.190828112504 /variable.yymmssHHMMSS
output(row,2) = 'yesboat';
elseif x==N
%store filename in output with 'no boat' in second column
output(row,1) = filename;
output(row,2) = 'noboat';
else
disp('Input must be Y or N!')
end
row=row+1;

채택된 답변

Stephen23
Stephen23 2019년 8월 29일
"...which is the best way to create an empty output for storing text info..."
Use a cell array or a string array, e.g.:
out = cell(N,2);
for row = 1:N
prompt='Can you see boat noise? Y/N';
x = input(prompt,'s'); % need 's'
switch upper(x)
case 'Y'
out{row,1} = filename;
out{row,2} = 'yesboat';
case 'N'
out{row,1} = filename;
out{row,2} = 'noboat';
otherwise
...
end
  댓글 수: 2
Louise Wilson
Louise Wilson 2019년 8월 29일
Thanks! I realised just after I posted that I am missing the 's'... and I did this...
str=string();
row=1;
for i=1:5
answer=input('Can you see boat noise? y/n: \n', 's')
if strcmp(answer,'y');
%store filename in output with 'boat' in second column
str(row,1) = 'filename';
str(row,2) = 'yesboat';
elseif strcmp(answer,'n');
%store filename in output with 'no boat' in second column
str(row,1) = 'filename';
str(row,2) = 'noboat';
else
disp('Input must be Y or N!')
end
row=row+1;
end
...however for some reason the code works but I get this printed in my command window each time... any ideas why this '8___ ' appears??
error.png
Stephen23
Stephen23 2019년 8월 29일
"...any ideas why this '8___ ' appears?"
It looks like MATLAB might be in some debugging mode. Do you have any breakpoints set?

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by