printing nxnxn matrix into a file

86 visualizaciones (últimos 30 días)
melih guneri
melih guneri el 18 de En. de 2024
Comentada: melih guneri el 19 de En. de 2024
Hi,
I have received a .mat file including a 3x3x2 matrix as follows;
****************************
X =
ans(:,:,1) =
1 0 1
1 1 0
0 0 1
ans(:,:,2) =
0 1 1
1 1 0
1 1 1
****************************
I'd like to print my 3x3x2 matrix into a .mat file in the format given in the example above.
It may be a very simple question, but how can I print my matrix to a .mat file in this format?
Thank you.
  1 comentario
Stephen23
Stephen23 el 18 de En. de 2024
Editada: Stephen23 el 18 de En. de 2024
"It may be a very simple question, but how can I print my matrix to a .mat file in this format?"
The question is not simple: MAT files are a binary file format:
Binary files do not store formatted text. They store numbers as bits.
What is your real goal?

Iniciar sesión para comentar.

Respuestas (3)

Image Analyst
Image Analyst el 18 de En. de 2024
You can't. The .mat file is a proprietary format, and I think it's binary. So the numbers are just in there and it doesn't make any sense to specify a format for them since a .mat file is not something you will ever look at like a text file. You will only use load to retrieve your variable from the .mat file.
Now if you want a text file, you can use fopen(), fprintf(), and fclose.

Steven Lord
Steven Lord el 18 de En. de 2024
It may be a very simple question, but how can I print my matrix to a .mat file in this format?
You're conflating two things. I'm going to make some arbitrary data A and apply the two methods to that data in a temporary directory. A is a random 3-by-3-by-2 array each element of which is an integer between 1 and 10.
cd(tempdir)
A = randi(10, [3 3 2])
A =
A(:,:,1) = 9 8 5 2 3 10 10 10 6 A(:,:,2) = 7 5 8 10 3 9 10 4 8
Do you want to save your array (a matrix must be 2-dimensional, as per the ismatrix function) to a binary MAT-file? If so use the save function.
save('mymatfile.mat', 'A')
whos -file mymatfile.mat
Name Size Bytes Class Attributes A 3x3x2 144 double
data = load('mymatfile.mat')
data = struct with fields:
A: [3×3×2 double]
disp(data.A)
(:,:,1) = 9 8 5 2 3 10 10 10 6 (:,:,2) = 7 5 8 10 3 9 10 4 8
Or do you want to print the display of this array to a text file? If so I'd use formattedDisplayText to capture the displayed text to a string then use writematrix to write that string to a file.
S = formattedDisplayText(A)
S =
" (:,:,1) = 9 8 5 2 3 10 10 10 6 (:,:,2) = 7 5 8 10 3 9 10 4 8 "
writematrix(S, 'mytextfile.txt')
type mytextfile.txt
" (:,:,1) = 9 8 5 2 3 10 10 10 6 (:,:,2) = 7 5 8 10 3 9 10 4 8 "
If you wanted to add the name of the variable to the string you could do that before writing it to the file.
S2 = replace(S, '(', 'A(')
S2 =
" A(:,:,1) = 9 8 5 2 3 10 10 10 6 A(:,:,2) = 7 5 8 10 3 9 10 4 8 "
  1 comentario
melih guneri
melih guneri el 19 de En. de 2024
Thank you Steven,
I'll try these solution.

Iniciar sesión para comentar.


Hassaan
Hassaan el 18 de En. de 2024
% Your 3x3x2 matrix
X = cat(3, [1, 0, 1; 1, 1, 0; 0, 0, 1], [0, 1, 1; 1, 1, 0; 1, 1, 1]);
% Open a new text file for writing
fileID = fopen('matrix_output.txt','w');
% Loop through each 'slice' of the matrix
for k = 1:size(X,3)
fprintf(fileID, 'ans(:,:,%d) =\n', k);
for i = 1:size(X,1)
fprintf(fileID, '\t');
for j = 1:size(X,2)
fprintf(fileID, '%d\t', X(i,j,k));
end
fprintf(fileID, '\n');
end
end
% Close the file
fclose(fileID);
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Feel free to contact me.
  3 comentarios
melih guneri
melih guneri el 19 de En. de 2024
Thank you Voss

Iniciar sesión para comentar.

Categorías

Más información sobre Shifting and Sorting Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by