Generating a 3D matrix from multiple tables

조회 수: 7 (최근 30일)
Kolleggerm1
Kolleggerm1 2020년 4월 15일
댓글: Kolleggerm1 2020년 4월 16일
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에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by