load and if conditions

1 回表示 (過去 30 日間)
mcm
mcm 2016 年 10 月 19 日
コメント済み: Walter Roberson 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 Center および File ExchangeCustom Training Loops についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by