Accesing vector data in for loop

3 visualizzazioni (ultimi 30 giorni)
Carla van der Merwe
Carla van der Merwe il 27 Set 2018
Modificato: Stephen23 il 27 Set 2018
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 Commento
Stephen23
Stephen23 il 27 Set 2018
Modificato: Stephen23 il 27 Set 2018
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');

Accedi per commentare.

Risposte (1)

Walter Roberson
Walter Roberson il 27 Set 2018
This is expected. Read the documentation on the output of load() applied to a mat file.

Categorie

Scopri di più su MATLAB Mobile in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by