load and if conditions
조회 수: 1 (최근 30일)
이전 댓글 표시
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
답변 (2개)
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
댓글 수: 0
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
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 Exchange에서 Custom Training Loops에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!