Generating a 3D matrix from multiple tables
9 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have 469 scripts that all generate a table "numericData". Each script specifies a different timestep.
I need to pull all the "'numericData" tables into one 3D matrix so that I can manipulate the data between different timesteps.
I wrote a code that loads the files from their location on my desktop, specifically for "numericData"
It is as follows
inputdir = 'C:\Users\kolleggerm1\Desktop\HMSPdryZ\Matlabfiles';
Files = dir(fullfile(inputdir,'.m'));
numfiles = length(Files);
mydata = cell(1, numfiles);
for k = 1:numfiles
mydata{k} = load(Files(k),numericData);
end
mydata is returning empty. I feel I am missing something simple that is preventing the data to be pulled together. Any suggestions?
2 commentaires
Tommy
le 15 Avr 2020
fullfile(inputdir,'.m')
This gives
'C:\Users\kolleggerm1\Desktop\HMSPdryZ\Matlabfiles\.m'
Perhaps you mean
>> fullfile(inputdir,'*.mat')
ans =
'C:\Users\kolleggerm1\Desktop\HMSPdryZ\Matlabfiles\*.mat'
?
Réponse acceptée
Stephen23
le 16 Avr 2020
Modifié(e) : Stephen23
le 16 Avr 2020
'I have 469 scripts that all generate a table "numericData"... I wrote a code that loads the files...'
There appears to be some confusion about the difference between data files (which can be loaded) and script/functions (which can be run or called):
- Scripts contain code. Scripts are run (normally they are run by simply writing the name of the script).
- Data (e.g. from a .mat file) can be loaded into MATLAB memory (e.g. by calling load).
Assuming that each script generates a variable with exactly the same name**, you will need to do something like this:
D = 'C:\Users\kolleggerm1\Desktop\HMSPdryZ\Matlabfiles';
S = dir(fullfile(D,'*.m'));
N = numel(S);
C = cell(1,N);
for k = 1:N
F = fullfile(D,S(k).name);
clearvars numericData
run(F) % RUN the script (NOT loading)
C{k} = numericData; % store the data
end
A = cat(3,C{:}); % concatenate into 3D array
** This is also a good example of why using scripts is NOT recommended: consider what would happen if one of the scripts does not generate that variable (e.g. spelling error, etc.), the code could easily continue to use the variable from the previous iteration without any warning whatsoever. One workaround would be to clearvars that variable at the start of each loop iteration (as shown above), but this comes at the expense of efficiency, and is still rather fragile.
Simpler and more robust would be to write functions and return that table as an output argument.
Even better would be to store data in data files.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Data Import and Analysis dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!