0

I am new to MATLAB and try to run a loop within a loop. I define a variable ID beforehand, for example ID={'100'}. In my loop, I then want to go to the ID's directory and then load the matfile in there. However, whenever I load the matfile, suddenly the ID definition gets overridden by all possible IDs (all folders in the directory where also the ID 100 is). Here is my code - I also tried fullfile, but no luck so far:

ID={'100'}

for subno=1:length(ID)     % first loop
  try         
   for sessno=1:length(Session) %  second loop, for each ID there are two sessions   
    subj_name =  ([ID{subno} '_' Session{sessno} '_vectors_SC.mat']);
    cd(['C:\' ID{subno} '\' Session{sessno}]);
    load(subj_vec_name) % the problem occurs here, when loading, not before
    end
  end
end

When I then check the length of the IDs, it is now not 1 (one ID, namely 100), but there are all possible IDs within the directory where 100 also lies, and the loop iterates again for all other possible IDs (although it should stop after ID 100).

Suever
  • 64,497
  • 14
  • 82
  • 101
LaNeu
  • 105
  • 14
  • As you have noted, when you use plain `load`, all the variables in the `.mat` file are put into your current workspace. If the file opened also contains a variable called `ID`, that will take precedence over the one you have defined. Try loading the data to a name variable instead: `Data = load(filename);`. You can then access the `ID` variable just loaded through `Data.ID`, and same for other variables in the file. – mikkola Apr 15 '16 at 14:52
  • Dear Mikkola, thank you very much, this was the problem - in the .mat file, there was also a variable called ID, so that is why the initial variable got overridden! – LaNeu Apr 15 '16 at 15:35

2 Answers2

5

You should always specify an output to load to prevent over-writing variables in your workspaces and polluting your workspace with all of the contents of the file. There is an extensive discussion of some of the strange potential side-effects of not doing this here.

Chances are, you have a variable named ID inside of the .mat file and it over-writes the ID variable in your workspace. This can be avoided using the output of load.

The output to load will contain a struct which can be used to access your data.

data = load(subj_vec_name);

%// Access variables from file
id_from_file = data.ID;

%// Can still access ID from your workspace!
ID

Side Note

It is generally not ideal to change directories to access data. This is because if a user runs your script, they may start in a directory they want to be in but when your program returns, it dumps them somewhere unexpected.

Instead, you can use fullfile to construct a path to the file and not have to change folders. This also allows your code to work on both *nix and Windows systems.

subj_name =  ([ID{subno} '_' Session{sessno} '_vectors_SC.mat']);

%// Construct the file path
filepath = fullfile('C:', ID{subno}, Session{sessno}, subj_name);

%// Load the data without changing directories
data = load(filepath);
Community
  • 1
  • 1
Suever
  • 64,497
  • 14
  • 82
  • 101
  • Also you can use `S = load(, )` to just get the variable you need if more are present in the file. – Matt Apr 15 '16 at 15:00
1

With the command load(subj_vec_name) you are loading the complete mat file located there. If this mat file contains the variable "ID" it will overwrite your initial ID. This should not cause your outer for-loop to execute more than once. The vector 1:length(ID) is created by the initial for-loop and should not get overwritten by subsequent changes to length(ID).

If you insert disp(ID) before and after the load command and post the output it might be easier to help.

Stefan
  • 353
  • 2
  • 11