Generating a 3D matrix from multiple tables

9 次查看(过去 30 天)
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 个评论
Tommy
Tommy 2020-4-15
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'
?
Kolleggerm1
Kolleggerm1 2020-4-16
It is still not working at all. Perhaps I need to write a script that opens and runs all of the individual scripts, saves the table, then combines all the tables into the 3D matrix.

请先登录,再进行评论。

采纳的回答

Stephen23
Stephen23 2020-4-16
编辑:Stephen23 2020-4-16
'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.
  1 个评论
Kolleggerm1
Kolleggerm1 2020-4-16
Scripts was not my choice...it is how the data was available in the repository I'm using. Its been immeasureably difficult to manipulate. Thank you!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Import and Analysis 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by