Dynamically loading mat files in nested loop

1 次查看(过去 30 天)
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

采纳的回答

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

更多回答(1 个)

Walter Roberson
Walter Roberson 2016-7-28
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 个评论
ALZ
ALZ 2016-7-28
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Whos 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by