How to read a single parameter value from multiple .mat files and store them in a new [n,1] (single column) array

4 views (last 30 days)
Good Afternoon,
For starters, the main goal of this script would be to loop for n= number of .mat files in a directory while finding a single value under a specified variable common to all of the .mat files and aggregating these values into a new array that is [n,1].
Here is the script I'm currently stuck on:
pathname = ' ' SearchDirectory(pathname, '*Tarrays.mat'); %The .mat files holding the desired variable val
load([pathname '\FileDirectory.mat']);
for i=2:length(FileDirectory) %Ea.file,STARTING w/ 2nd one(since 1st=FDirectory itself)
load([pathname '\' strrep(FileDirectory(i,1).name, '.mat', '')]);
%Here is where I read in these variable values, BUT I'm stuck on how to create an array where the loop continuously is adding the said value so they can be saved as a single .mat array for the entire loop
SumAbs_DFM = find(Sum_STCplot_array_TfrMean_abs);
%'Sum_STCplot_array_TfrMean_abs' is the common variable amongst these .mat files
%Presumably this is where I specify the new matrix
end
save 'newmatrix' %Holding collection of the single variable values from each .mat file
An extra addition to the new matrix to preserve filenames of the .mat files where the 'Sum_STCplot_array_TfrMean_abs' values were read would also be useful, but I'm unsure where to begin as it would require another column in the new matrix
ANY help or pointers is much appreciated, as I haven't been able to find much online that I could connect with my issue!
Thank you very much!
-Reid
P.S. I still use Matlab 2012a

Answers (1)

dpb
dpb on 20 May 2014
Edited: dpb on 21 May 2014
pathname = ' ' SearchDirectory(pathname, '*Tarrays.mat');
load([pathname '\FileDirectory.mat']);
for i=2:length(FileDirectory)
load([pathname '\' strrep(FileDirectory(i,1).name, '.mat', '')]);
SumAbs_DFM = find(Sum_STCplot_array_TfrMean_abs);
%'Sum_STCplot_array_TfrMean_abs' is the common variable amongst these .mat files
end
Please format code in preview window before posting...
I don't know what pathname or SearchDirectory are; neither is found by a search of Matlab documentation.
I'll presume directory is in the pathname variable and the wildcard matches the .mat files wanted. If not, fix up both to match your configuration and layout.
d=dir(fullfile(pathname, '*Tarrays.mat'); % directory structure of desired files
for i=1:length(d) % the wildcard ensures are all .mat files
V=zeros(length(d),1); % preallocate for the resulting array
V(i)=load(fullfile(pathname,d(i).name, ...
'Sum_STCplot_array_TfrMean_abs'); % store value in V
end
save('YOURNEWFILENAME.MAT',V) % save the new array in a different file
To add the file name to the array would require either
a) making a cell array since one value would be numeric while another text, or
b) creating a second text array of same length that is the names and use the two together, or
c) using a structure array where one field is the value the other the name
Look up cell arrays and structures in the doc's for details on those options.

Categories

Find more on Search Path in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!