load and if conditions
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I need a for loop that will call a data set (1997 or 2013) and pull data from the data to calculate what each value if equal 2. The code I created doesn't run. But this is what I have so far. How can I improve it?
pick_year = input('Pick a year: either 1997 or 2013: ')
for pick_year
if pick_year == 1997
load('UPDRSscores_1997')
elseif pick_year == 2013
load('UPDRSscores_2013')
end
if pick_year == 0
display('normal')
elseif pick_year == 1
display('slight')
elseif pick_year == 2
display('mild')
elseif pick_year == 3
display('moderate')
elseif pick_year == 4
display('severe')
end
end
1 Kommentar
Antworten (2)
James Tursa
am 19 Okt. 2016
Maybe just get rid of that for-loop (assuming pick_year is one of the variables that gets loaded). E.g.,
pick_year = input('Pick a year: either 1997 or 2013: ')
if pick_year == 1997
load('UPDRSscores_1997')
elseif pick_year == 2013
load('UPDRSscores_2013')
else
error('Invalid year')
end
if pick_year == 0
display('normal')
elseif pick_year == 1
display('slight')
elseif pick_year == 2
display('mild')
elseif pick_year == 3
display('moderate')
elseif pick_year == 4
display('severe')
end
0 Kommentare
Walter Roberson
am 19 Okt. 2016
"poofing" a variable value from a load() can have different results with different MATLAB versions. Earlier it was well defined, but it is getting increasingly restricted. It is recommended that you always use load() with an output variable and pull the data out of the output variable.
pick_year = input('Pick a year: either 1997 or 2013: ')
filename = sprintf('UPDRSscores_%d.mat', pick_year);
if ~exist(filename, 'file')
error('file "%s" does not exist', filename);
end
datastruct = load(filename);
pick_year = datastruct.pick_year; %pull it out of what was loaded from the file
switch pick_year
case 0:
display('normal')
case 1:
display('slight')
case 2:
display('mild')
case 3:
display('moderate')
case 4:
display('severe')
otherwise:
error('stored pick_year had bad value %f\n', pick_year);
end
4 Kommentare
Walter Roberson
am 19 Okt. 2016
pick_year = input('Pick a year: either 1997 or 2013: ')
filename = sprintf('UPDRSscores_%d.mat', pick_year);
if ~exist(filename, 'file')
error('file "%s" does not exist', filename);
end
datastruct = load(filename);
pick_year = datastruct.pick_year; %pull it out of what was loaded from the file
if pick_year == 0
display('normal')
elseif pick_year == 1
display('slight')
elseif pick_year == 2
display('mild')
elseif pick_year == 3
display('moderate')
elseif pick_year == 4
display('severe')
else
error('stored pick_year had bad value %f\n', pick_year);
end
Or, better:
states = {'normal', 'slight', 'mild', 'moderate', 'severe'};
pick_year = input('Pick a year: either 1997 or 2013: ')
filename = sprintf('UPDRSscores_%d.mat', pick_year);
if ~exist(filename, 'file')
error('file "%s" does not exist', filename);
end
datastruct = load(filename);
pick_year = datastruct.pick_year; %pull it out of what was loaded from the file
if pick_year >= 0 && pick_year <= length(states) - 1 && mod(pick_year,1) == 0
display( states{pick_year + 1} );
else
error('stored pick_year had bad value %f\n', pick_year);
end
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!