Filter löschen
Filter löschen

load and if conditions

7 Ansichten (letzte 30 Tage)
mcm
mcm am 19 Okt. 2016
Kommentiert: Walter Roberson am 19 Okt. 2016
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
James Tursa
James Tursa am 19 Okt. 2016
is pick_year actually one of the variables that gets loaded?

Melden Sie sich an, um zu kommentieren.

Antworten (2)

James Tursa
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

Walter Roberson
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
mcm
mcm am 19 Okt. 2016
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 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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Just for fun finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by