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

5 Ansichten (letzte 30 Tage)
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;

Akzeptierte Antwort

Stephen23
Stephen23 am 29 Aug. 2019
"...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 Kommentare
Louise Wilson
Louise Wilson am 29 Aug. 2019
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 am 29 Aug. 2019
"...any ideas why this '8___ ' appears?"
It looks like MATLAB might be in some debugging mode. Do you have any breakpoints set?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Data Type Identification finden Sie in Help Center und File Exchange

Produkte


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by