How to load multiple .mat files which are having subfile in .mat format using the loop?
Afficher commentaires plus anciens
How to load multiple .mat files which are having subfile in .mat format using the loop?
1 commentaire
Sarvesh Kale
le 7 Fév 2023
You can also import the mat files in directory using the import data option found under Home tab of MATLAB
Réponse acceptée
Plus de réponses (3)
Sarvesh Kale
le 7 Fév 2023
As per my understanding you are trying to import a MAT file which has multiple data inside it, you can use the following script
function importfile(fileToRead1)
newData1 = load('-mat', fileToRead1);
% Create new variables in the base workspace from those fields.
vars = fieldnames(newData1);
for i = 1:length(vars)
assignin('base', vars{i}, newData1.(vars{i}));
end
Save the above code as a file named importfile.m
Now you should place your MAT files in the same location as this importfile.m script, then simply do the following
importfile('B007 1.mat'); % will load everything inside the mat file
importfile('B007 0.mat'); % same as above
This will load the data in the MATLAB workspace, I hope this answers your queries, please accept the answer if it does.
Thank you
3 commentaires
Mathieu NOE
le 7 Fév 2023
hmm assignin , really ?
@Mathieu NOE: The author of this code also does not deal with the inevitable mess of variables that the OP will then have in their workspace... showing that once again, magical variable names just cause more problems.
Sadly there seems to be no requirement for TMW staff to learn or recommend efficient or robust MATLAB programming. At the bare minimum, some links to the MATLAB documentation would be helpful:
"Assigning to variables in the caller workspace can make code more difficult to understand, give surprising results to the user (unexpected or redefined variables in their workspace), and have a negative performance impact. The best practice is to have the function return the variables as output arguments."
Sarvesh Kale
le 7 Fév 2023
I appreciate @Stephen23 for recommending good MATLAB practices and a descriptive answer, I will try to follow them. I am also learning and growing my MATLAB knowledge everyday ! thanks to people like you.
Mathieu NOE
le 7 Fév 2023
hello
I suspect you wante to store the data in a cell array (and not a regular numeric array)
so try this :
matData = 1×2 cell array
{1×1 struct} {1×1 struct}
>> matData{:} struct with fields:
X118_DE_time: [122571×1 double]
X118_FE_time: [122571×1 double]
X118_BA_time: [122571×1 double]
X118RPM: 1796
struct with fields:
X119_DE_time: [121410×1 double]
X119_FE_time: [121410×1 double]
X119_BA_time: [121410×1 double]
X119RPM: 1772
myFolder = 'C:\Users\IIR\Documents\Machine data\12K Drive End Bearing Dataset'; % Define your working folder
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
filePattern = fullfile(myFolder, '*.mat');
matFiles = dir(filePattern);
for k = 1:length(matFiles)
baseFileName = matFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
matData{k} = load(fullFileName); % <= here my mod
end
Sai Sumanth Korthiwada
le 7 Fév 2023
Hi Rajeev Kumar,
I understand that you wanted to load multiple '.mat' files which are having subfile in '.mat' format using the loop. To achieve this, please prefer using 'cell array' instead of 'struct'. The error is due to inconsistent sizes of the fileds in the stuct 'matData'.
Please refer to the modified code, where 'matData' is defined as 'cell array' and fields are added into the 'cell array'.
myFolder = 'C:\Users\skorthiw\Downloads\test'; % Define your working folder
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
filePattern = fullfile(myFolder, '*.mat');
matFiles = dir(filePattern);
matData = {}; % cell array
for k = 1:length(matFiles)
baseFileName = matFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
matData(k) = {load(fullFileName)}; % data added into cell array
end
%% to access the data in the cell array:
matData{1}.X118_BA_time;
matData{2}.X119_FE_time;
Hope this helps.
Catégories
En savoir plus sur Environment and Settings dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!