Accesing vector data in for loop
8 次查看(过去 30 天)
显示 更早的评论
I want to access the vector data from several variables in a for loop. I have managed to build the string to load the filename and variables, but the actual data is not accessed.
this is the code:
vars={var1,var2,var3,var4};
for ii=1:length(vars)
file=dir(fullfile(maindirectory,'mydata.mat'));
filename=file.name
fullfilename=strcat(maindirectory,'\',filename);%builds the string
Data=load(fullfilename,vars{ii});
end
it all falls apart at Data.
The code just loads the struct with fields (var1: [99x1 double]), but not the actual data.
1 个评论
Stephen23
2018-9-27
编辑:Stephen23
2018-9-27
This is very confusing: it is not clear why you call dir when you already know the exact filename:
file=dir(fullfile(maindirectory,'mydata.mat'));
filename=file.name
fullfilename=strcat(maindirectory,'\',filename);%builds the string
Perhaps you just meant to do this:
fullfilename = fullfile(maindirectory,'mydata.mat');
回答(1 个)
Walter Roberson
2018-9-27
This is expected. Read the documentation on the output of load() applied to a mat file.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!