How to load multiple .mat files which are having subfile in .mat format using the loop?
3 views (last 30 days)
Show older comments
How to load multiple .mat files which are having subfile in .mat format using the loop?
1 Comment
Sarvesh Kale
on 7 Feb 2023
You can also import the mat files in directory using the import data option found under Home tab of MATLAB
Accepted Answer
Stephen23
on 7 Feb 2023
Edited: Stephen23
on 7 Feb 2023
Unfortunately the data in those MAT files is very badly designed, because each MAT file uses a changing prefix on the variable names. Ugh. Much better would be if the variable names were exactly the same in every MAT file, then your code would be simpler, more efficient, and much more robust.
Do NOT use ASSIGNIN() or try to LOAD() all of those variables directly into the workspace: this will just make the rest of your code slow and complex when you try to access those variables.
But with some effort we can work with what you have:
P = '.'; % absolute or relative path to where the files are saved.
S = dir(fullfile(P,'B007*.mat'))
for k = 1:numel(S)
F = fullfile(S(k).folder,S(k).name);
D = load(F) % ugh ugh ugh, that ugly prefix on those poor variable names!
D = cell2struct(struct2cell(D),regexprep(fieldnames(D),'^\w\d+_?',''),1); % much better.
S(k).data = D;
end
D = [S.data] % aaah, now that is the best way to store your imported data!
Now you can trivially access your data using indexing and fieldnames. For example, the second file:
S(2).name
D(2).DE_time
D(2).RPM
2 Comments
Mathieu NOE
on 9 Feb 2023
it's quite a bit unclear to me
seems that for the time being you have posted two data (mat) files .
what do you mean by "main" mat file and subfile ? the two provided files do not seems to have nay hierarchical relationship ... or am I wrong ?
More Answers (3)
Sarvesh Kale
on 7 Feb 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 Comments
Stephen23
on 7 Feb 2023
Edited: Stephen23
on 7 Feb 2023
@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
on 7 Feb 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
on 7 Feb 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
0 Comments
Sai Sumanth Korthiwada
on 7 Feb 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.
0 Comments
See Also
Categories
Find more on Startup and Shutdown 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!