Generating a 3D matrix from multiple tables

6 views (last 30 days)
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 Comments
Tommy
Tommy on 15 Apr 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'
?
Kolleggerm1
Kolleggerm1 on 16 Apr 2020
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.

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 16 Apr 2020
Edited: Stephen23 on 16 Apr 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.
  1 Comment
Kolleggerm1
Kolleggerm1 on 16 Apr 2020
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!

Sign in to comment.

More Answers (0)

Categories

Find more on Data Import and Analysis in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!