Dynamically loading mat files in nested loop

2 views (last 30 days)
ALZ
ALZ on 28 Jul 2016
Edited: ALZ on 5 Aug 2016
Hi, I have several mat files in different directory, the names are organized in the following manner
"Coluomb_Torque_XX_Speed_YY" where XX = 1:29 and YY=1:31;
the problem when I create a nested loop and try to load the files by their full name (including the subdirectories), I get an error regarding the index. I appreciate anyone's help
clear
clc
XX=1:29;
YY=1:31;
for q=1:length(XX)
for p=1:length(YY)
ff='Coluomb_';
tt='Torque_'; %
uu='_Ratio_'; %
qq=num2str(q);
pp=num2str(p);
filename=[ff,tt,qq,uu,pp];
filename = ['C:\Users\folder1\folder2\folder3\folder4\Data Generating\Coluomb\' filename]
load(filename)
%%%%Do your Processing
% figure(1)
% plot(t,t1)
% hold on
% Clear filename from Workspace
end
end
The error am getting is :
Error using load
Unable to read file
'C:\Users\folder1\folder2\folder3\folder4\DataGenerating\Coluomb\. No such file or
directory.
Error in post_proc (line 18)
load(filename)
as you can see, the loop keeps going beyond the index 29, and this happens only when I use the Load command, i.e if I remove the load command I get the filenames exactly as they should be.
Thanks
Alz

Accepted Answer

ALZ
ALZ on 28 Jul 2016
It is solved, I had to include " qq=num2str(q);" in the outer for loop
Thanks

More Answers (1)

Walter Roberson
Walter Roberson on 28 Jul 2016
Avoid using load() in that form. Instead assign the output of load() to a variable. The result (for a .mat file) will be a structure with one field for each variable stored in the .mat file; you would access the appropriate field of that structure.
When you use load() in the form you have, you might end up loading a new value for one of your other variables. For example there might happen to be a variable named "p" or "YY" in the .mat file and those would overwrite the values you had carefully set. That kind of magic overwriting is not possible if you assign the output of load to a variable.
  1 Comment
ALZ
ALZ on 28 Jul 2016
you are right Walter, in this code am over writing mat files and the code seems slow for post-processing large data. I'll go with your suggestion. Thanks

Sign in to comment.

Categories

Find more on File Operations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!