Unable to load multiple mat files

Hello, I have a bunch of mat files as shown below. I feel I've done something similar before but now this is driving me slightly crazy...
I'm trying to load the data from the multiple mat files into a structure or any other easily indexable/accessible format.
This is my code
myFolder = 'C:\Users\dbhadra\Desktop\MSD\M150_Actuation_Accuracy\HittechProto3_RawData\output';
theFiles = dir(fullfile(myFolder, '/*M150*.mat'));
S = struct([]);
for i=1:numel(theFiles)
S{i} = load(theFiles(i).name);
end
I keep getting errors, most common being below
Error using load
Unable to read MAT-file
C:\Users\dbhadra\Desktop\MSD\M150_Actuation_Accuracy\HittechProto3_RawData\output\2018-10-25_10-56-15_TEST_20_M150RawData_part1.mat.
Not a binary MAT-file. Try load -ASCII to read as text.
The funny is this seems to work standalone, except not in a loop. Where am I going wrong? Thanks.

Réponses (2)

Jan
Jan le 25 Fév 2022
This does not match:
S = struct([]);
for i=1:numel(theFiles)
S{i} = ...
You define S as a struct, but then you create cell elements. Either use S(i) or initialize S by:
S = cell(1, numel(theFiles));
The path is missing in the load command:
S{i} = load(fullfile(myFolder, theFiles(i).name));
But then the current folder must contain a file with the given name in a not matching format. This is at least strange.
Please openthe file manually and check the format. Are they ASCII-files?

5 commentaires

Hey @Jan: thanks for your reply. Even with
S(i) = load(fullfile(myFolder,theFiles(i).name));
I still get an error. Note that I was already in the folder where the .mat files are located. The above is overkill.
Subscripted assignment between dissimilar structures.
How do I tell if they are ASCII files? It just looks like an usual .mat file to me. When I load a single one in workspace. All the other .mat files are similar with varying no. of columns.
I even tried the alternate
S = cell(1, numel(theFiles));
but I still get the same error as in my original post.
Jan
Jan le 26 Fév 2022
"Note that I was already in the folder where the .mat files are located. The above is overkill." - no, it is not. Remember that a callback of a timer or GUI object can call cd() to change the current folder. This is a frequent source of run time errors, which are extemelly hard to debug. Therefore it is a good programming practice to use the absolute path in every case and not to use cd or relying on the current folder at all.
"How do I tell if they are ASCII files?" - Open the MAT file in an editor. You see directly, if this is ASCII (you can read everything) or binary (looking weird).
Of course storing the results in a cell does not solve the problem, that the MATfiles do not have the correct format to be imported.
Deepayan Bhadra
Deepayan Bhadra le 28 Fév 2022
Modifié(e) : Deepayan Bhadra le 28 Fév 2022
@Jan: "You see directly, if this is ASCII (you can read everything) or binary (looking weird)" -> seems to be binary but this contradicts the initial error that we see.
Stephen23
Stephen23 le 28 Fév 2022
"but this contradicts the initial error that we see. "
No, it does not.
The error message states "Not a binary MAT-file."
Your file is a binary file, but that does not mean that it is a MATLAB MAT-file.
Jan
Jan le 1 Mar 2022
@Deepayan Bhadra: As Stephen said: This is not a binary MAT file. Binary MAT files starts with some readable text like "MATLAB 5.0 MAT-file, Platform: PCWIN64, Created on: Mon Feb 28 12:42:18 2022" before the binary stuff starts. Your file does not. So the error message is correct.
Which software has created the files?

Connectez-vous pour commenter.

Stephen23
Stephen23 le 25 Fév 2022
Modifié(e) : Stephen23 le 26 Fév 2022
theFiles is already a structure, there is no point in creating another one when you can use theFiles already:
myFolder = 'C:\Users\dbhadra\Desktop\MSD\M150_Actuation_Accuracy\HittechProto3_RawData\output';
theFiles = dir(fullfile(myFolder, '*M150*.mat'));
for k = 1:numel(theFiles)
theFiles(k).data = load(theFiles(i).name);
end
data = [theFiles.data] % optional, assumes all file contain the same variables.

2 commentaires

Hi Stephen, I tried this, even removing the / from the .mat filename. Still the same error
Not a binary MAT-file. Try load -ASCII to read as text.
Stephen23
Stephen23 le 1 Mar 2022
"Still the same error"
Still the same files: not binary MAT files.

Connectez-vous pour commenter.

Catégories

Produits

Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by