Different variables depending on which file is loaded

Hi,
I have two text files named SIXRX1.txt and SIXRX2.txt, and both of them should be able to load into a script (but not simultaneously). So I want to be able to type load SIXRX1.txt OR load SIXRX2.txt at the top of my script. However, I need certain variables to change depending on which text file I load.
%If SIXRX2.txt is loaded, the variables should be
year1=1993;
year2=1998;
%And if SIXRX1.txt is loaded, the variables should be
year1=1999;
year2=2017;
How do I write this? Note: I plan on manually changing the number in the "Load SIXRX().txt" function, but I don't want to change the year variables aswell.

 Accepted Answer

Put the filename in a variable, and use switch:
fnm = 'SIXRX2.txt';
S = load(fnm);
...
switch fnm
case 'SIXRX1.txt'
year1 = ...
year2 = ...
case 'SIXRX2.txt'
year1 = ...
year2 = ...
otherwise
error('This file is not recognized: %s',fnm)
end
You should avoid calling load using command syntax, which would also allow you to turn your script into a much better function and provide the filename as an input argument.
Also always load into a variable, as shown above, to avoid these problems:

2 Comments

Works perfectly, thank you very much!
@Olle Christenson: I hope that it helps :)

Sign in to comment.

More Answers (0)

Categories

Tags

Community Treasure Hunt

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

Start Hunting!