How can I load a .mat struct?

Hello community:
I use Matlab 2021a and when I try to load a .mat struct with these sentences load('linesStruct_1.mat') or load('linesStruct_1') I receive the following message:
Error using load
Unable to read MAT-file C:\ ... linesStruct_1.mat. Not a binary MAT-file. Try load -ASCII to
read as text.
Error in drawpoint_Algoritmo_Rename_Struct_Pendiente (line 5)
load('linesStruct_1.mat');
Could it be because I have saved the structures with a different name (lines in place of linesStruct_1.mat) than the image? Or maybe it has to do with the shortcut icons that appear in my folders?.
I remember this was not happening in other versions. I have been reading the many cases in which this occurs but I can not find a solution according to my situation. Any idea?
Thank you very much.

 Accepted Answer

The file is not a .mat file. At the moment we do not know what it is. Experiment with
filename = 'linesStruct_1.mat';
if isunix()
filename = 'handel.mat';
end
[fid, msg] = fopen(filename, 'r');
if fid < 0
error('could not open file "%s" because "%s"', filename, msg);
end
bytes = fread(fid, [1 100], '*uint8');
fclose(fid)
ans = 0
fprintf('first part of file as text:\n');
first part of file as text:
fprintf('%s\n', char(bytes));
MATLAB 5.0 MAT-file, Platform: GLNX86, Created on: Tue Mar 9 11:01:23 2004
fprintf('\nas decimal: \n');
as decimal:
disp(bytes);
Columns 1 through 33 77 65 84 76 65 66 32 53 46 48 32 77 65 84 45 102 105 108 101 44 32 80 108 97 116 102 111 114 109 58 32 71 76 Columns 34 through 66 78 88 56 54 44 32 67 114 101 97 116 101 100 32 111 110 58 32 84 117 101 32 77 97 114 32 32 57 32 49 49 58 48 Columns 67 through 99 49 58 50 51 32 50 48 48 52 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 Column 100 32
fprintf('\nas hex: \n');
as hex:
fprintf('%02x ', bytes);
4d 41 54 4c 41 42 20 35 2e 30 20 4d 41 54 2d 66 69 6c 65 2c 20 50 6c 61 74 66 6f 72 6d 3a 20 47 4c 4e 58 38 36 2c 20 43 72 65 61 74 65 64 20 6f 6e 3a 20 54 75 65 20 4d 61 72 20 20 39 20 31 31 3a 30 31 3a 32 33 20 32 30 30 34 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fprintf('\n');
The bit about isunix() is my substituting a different file for the purpose of demonstration here.
You are hoping to see the text version start with 'MATLAB 5.0' or 'MATLAB 7.3' . If you show us the three parts of the output we might be able to tell you what kind of file you actually have instead of a .mat file.

7 Comments

Hello again.
I show you the results obtained. I include the code with I saved the files. I apologize for not respecting the correct setting of the live script.
Name = sprintf("linesStruct_%i.mat", i);
Thanks in advance.
filename = 'linesStruct_1.mat';
if isunix()
filename = 'handel.mat';
end
[fid, msg] = fopen(filename, 'r');
if fid < 0
error('could not open file "%s" because "%s"', filename, msg);
end
bytes = fread(fid, [1 100], '*uint8');
fclose(fid)
ans = 0
fprintf('first part of file as text:\n');
first part of file as text:
fprintf('%s\n', char(bytes));
ÿØÿà JFIF ÿÛ
$.' ",#(7),01444'9=82<.342ÿÀ
fprintf('\nas decimal: \n');
as decimal:
disp(bytes);
Columns 1 through 32
255 216 255 224 0 16 74 70 73 70 0 1 1 0 0 1 0 1 0 0 255 219
0 67 0 8 6 6 7 6 5 8
Columns 33 through 64
7 7 7 9 9 8 10 12 20 13 12 11 11 12 25 18 19 15 20 29 26 31 30
29 26 28 28 32 36 46 39 32
Columns 65 through 96
34 44 35 28 28 40 55 41 44 48 49 52 52 52 31 39 57 61 56 50 60 46
51 52 50 255 192 0 11 8 1 0
Columns 97 through 100
1 0 1 1
fprintf('\nas hex: \n');
as hex:
fprintf('%02x ', bytes);
ff d8 ff e0 00 10 4a 46 49 46 00 01 01 00 00 01 00 01 00 00 ff db 00 43 00 08 06 06 07 06 05 08 07 07 07 09 09 08 0a 0c 14 0d 0c 0b 0b 0c 19 12 13 0f 14 1d 1a 1f 1e 1d 1a 1c 1c 20 24 2e 27 20 22 2c 23 1c 1c 28 37 29 2c 30 31 34 34 34 1f 27 39 3d 38 32 3c 2e 33 34 32 ff c0 00 0b 08 01 00 01 00 01 01
fprintf('\n');
That 'JFIF' tells me that you somehow saved a JPEG file over top of your .mat file. And indeed https://www.ntfs.com/jpeg-signature-format.htm says that JPEG start with hex FF D8 FF, which your file does.
Hello.
I think I have found the solution in this post:
I have been reading about the difference between images and figures. If I take account the used code, I should use saveas or savefig only after modifying the code. However, I understand that keyboard command will still be a problem.
I will keep working on this.
Thank you very much.
clear;
clc;
c=cell(10,1);
for i=1:1
c{i}=imread(sprintf('Mexico_Resize_%d.jpg',i));
columns = 256; rows = 256;
I1 = c{i};
imshow(I1);
hold on
I1 = imresize(I1, [columns rows]);
x1 = 64; y1 = 64; x2 = 64; y2 = 128; x3 = 64; y3 = 192;
x4 = 192; y4 = 64; x5 = 192; y5 = 128; x6 = 192; y6 = 192;
roi1= drawpoint('Position', [x1, y1], 'Color','b');
event.listener(roi1, 'ROIMoved', @allevents_1);
roi2= drawpoint('Position', [x2, y2], 'Color','g');
event.listener(roi2, 'ROIMoved', @allevents_2);
roi3= drawpoint('Position', [x3, y3], 'Color','w');
event.listener(roi3, 'ROIMoved', @allevents_3);
roi4= drawpoint('Position', [x4, y4], 'Color','c');
event.listener(roi4, 'ROIMoved', @allevents_4);
roi5= drawpoint('Position', [x5, y5], 'Color','r');
event.listener(roi5, 'ROIMoved', @allevents_5);
roi6= drawpoint('Position', [x6, y6], 'Color','m');
event.listener(roi6, 'ROIMoved', @allevents_6);
% Stop at each iteration to move the points:
keyboard;
dataA(1,1) = roi1.Position(1,1); dataA(1,2) = roi1.Position(1,2);
dataA(2,1) = roi2.Position(1,1); dataA(2,2) = roi2.Position(1,2);
dataB(1,1) = roi2.Position(1,1); dataB(1,2) = roi2.Position(1,2);
dataB(2,1) = roi3.Position(1,1); dataB(2,2) = roi3.Position(1,2);
dataC(1,1) = roi4.Position(1,1); dataC(1,2) = roi4.Position(1,2);
dataC(2,1) = roi5.Position(1,1); dataC(2,2) = roi5.Position(1,2);
dataD(1,1) = roi5.Position(1,1); dataD(1,2) = roi5.Position(1,2);
dataD(2,1) = roi6.Position(1,1); dataD(2,2) = roi6.Position(1,2);
plot(dataA(:, 1), dataA(:, 2), 'LineWidth',2,'Color','green');
plot(dataB(:, 1), dataB(:, 2), 'LineWidth',2,'Color','green');
plot(dataC(:, 1), dataC(:, 2), 'LineWidth',2,'Color','green');
plot(dataD(:, 1), dataD(:, 2), 'LineWidth',2,'Color','green');
pendienteA = (dataA(2,2) - dataA(1,2))/(dataA(2,1) - dataA(1,1));
pendienteB = (dataB(2,2) - dataB(1,2))/(dataB(2,1) - dataB(1,1));
pendienteC = (dataC(2,2) - dataC(1,2))/(dataC(2,1) - dataC(1,1));
pendienteD = (dataD(2,2) - dataD(1,2))/(dataD(2,1) - dataD(1,1));
lines(1).point1 = [dataA(1,1), dataA(1,2)]; lines(1).point2 = [dataA(2,1),dataA(2,2)];
lines(2).point1 = [dataB(1,1), dataB(1,2)]; lines(2).point2 = [dataB(2,1),dataB(2,2)];
lines(3).point1 = [dataC(1,1), dataC(1,2)]; lines(3).point2 = [dataC(2,1),dataC(2,2)];
lines(4).point1 = [dataD(1,1), dataD(1,2)]; lines(4).point2 = [dataD(2,1),dataD(2,2)];
lines(1).pendiente = pendienteA;
lines(2).pendiente = pendienteB;
lines(3).pendiente = pendienteC;
lines(4).pendiente = pendienteD;
dim = I1(1:256, 1:256);
Name = sprintf("linesStruct_%i.mat", i);
CutOut = I1(1:256, 1:256);
imwrite(CutOut, Name, 'jpg'); % This is not correct.
keyboard;
imshow(CutOut);
close all
end
Use
Name = sprintf("linesStruct_%i.jpg", i);
if you are going to be using imwrite()
If you do not actually want to write the data as an image file, you could also save the data into a .mat file. For that you could do something like
varname = sprintf("linesStruct_%i", i);
clear tosave
tosave.(varname) = CutOut;
save('all_linesStruct.mat', '-append', '-struct', 'tosave')
This would append the data to all_lineStruct.mat as a variable named linesStruct_ followed by a number.
David Ruiz Velasco
David Ruiz Velasco on 29 Jun 2021
Edited: David Ruiz Velasco on 29 Jun 2021
Hello.
My idea consist of save each struct's data in a .mat file to be able to remember its relationship with the numbering of each image. I have got it.
I'll contextualize the situation in case I can help someone.
Previously, I developed an algorithm that does the above using the Hough transform, but to do this I had to save and load sections of the main image as the polyline grew so that the Hough transform only focuses on the region of interest. This avoids the use of post segmentation. Now I try to use that algorithm without clipping. Obviously this makes the polyline barely evolve so I try to store it in a .mat file to use designing a custom training method.
Up to this point, I can be content with saving the data independently of the image, although I understand that depending on the strategy to adopt, perhaps I should use some other storage method that allows me to associate polylines and images more directly.
Thak you very much.
I am not clear as to whether the problem is solved?
Hello.
Yes, it is solved.
Thank you very much.

Sign in to comment.

More Answers (0)

Products

Release

R2021a

Community Treasure Hunt

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

Start Hunting!