load and if conditions

2 次查看(过去 30 天)
mcm
mcm 2016-10-19
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 个评论
James Tursa
James Tursa 2016-10-19
is pick_year actually one of the variables that gets loaded?

请先登录,再进行评论。

回答(2 个)

James Tursa
James Tursa 2016-10-19
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

Walter Roberson
Walter Roberson 2016-10-19
"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 个评论
mcm
mcm 2016-10-19
The code you just wrote, does not work. How can I improve it. There is a problem with this line pick_year = datastruct.pick_year; %pull it out of what was loaded from the file
how should i change it?
Walter Roberson
Walter Roberson 2016-10-19
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Just for fun 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by