Load multiple *.mat files and save outputs using loop without overwriting the previous file

6 visualizzazioni (ultimi 30 giorni)
I'm new to Matlab. The goal of my code is to load *.mat files from a given folder and perform the following:
  • Load the data from each file
  • Output the file name excluding the path for each file to be later used in a plot legend
I can do this for each file individually but using my for loop, the previous file is overwitten and I end up with only one output (the last file in the folder).
For the file name output, I have attempted to create a cell array named "Out" but I get the following error: Unable to perform assignment because the left and right sides have a different number of elements. I know that the size of my cell aray need to match the size of the output but I'm not sure how to create a cell array of the right size when the outputs are different for each file.
This still doesn't address the data itself being overwritten during each iteration of the loop.
% Specify the folder where the files live.
myFolder = 'enter folder directory here';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s\nPlease specify a new folder.', myFolder);
uiwait(warndlg(errorMessage));
myFolder = uigetdir(); % Ask for a new one.
if myFolder == 0
% User clicked Cancel
return;
end
end
filePattern = fullfile(myFolder, '*.mat');
theFiles = dir(filePattern);
% n=theFiles.name(1).Value
x=size(theFiles);
Out = zeros(length(theFiles),1);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name
fullFileName = fullfile(theFiles(k).folder, baseFileName)
[~,name,~] = fileparts(fullFileName)
load(fullFileName)
Out(k)=basefilename
end
Thanks in advance. Sorry if I left anything important out.

Risposta accettata

Jan
Jan il 28 Set 2022
Modificato: Jan il 28 Set 2022
filePattern = fullfile(myFolder, '*.mat');
theFiles = dir(filePattern);
nFiles = numel(theFiles); % not size()
Out = cell(nFiles, 1); % Not zeros()
for k = 1:nFiles
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
load(fullFileName)
[~, name] = fileparts(fullFileName);
Out{k} = name;
end
Loading data directly into the workspace is fragile: If the MAT file contains a variable called "Out" or "theFiles", the code might crash. Overwriting of formerly loaded variables is possible also. It is safer to store the loaded variables in an array or cell array.
  2 Commenti
Grace Hackenberg
Grace Hackenberg il 28 Set 2022
Thanks for the help! I am able to extract the file names individually now but the data from each file still overwrites the previous file and I am left with only one set of variables from the last file to run through the loop. Any ideas?
Jan
Jan il 29 Set 2022
As I have written already: It is safer to store the loaded variables in an array or cell array. E.g.
data = cell(1, nFiles);
for k = 1:nFiles
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
data{k} = load(fullFileName);

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su MATLAB in Help Center e File Exchange

Prodotti


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by