Generating a 3D matrix from multiple tables
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Kolleggerm1
el 15 de Abr. de 2020
Comentada: Kolleggerm1
el 16 de Abr. de 2020
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 comentarios
Tommy
el 15 de Abr. de 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'
?
Respuesta aceptada
Stephen23
el 16 de Abr. de 2020
Editada: Stephen23
el 16 de Abr. de 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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Data Import and Analysis en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!