printing nxnxn matrix into a file

81 Ansichten (letzte 30 Tage)
melih guneri
melih guneri am 18 Jan. 2024
Kommentiert: melih guneri am 19 Jan. 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 Kommentar
Stephen23
Stephen23 am 18 Jan. 2024
Bearbeitet: Stephen23 am 18 Jan. 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?

Melden Sie sich an, um zu kommentieren.

Antworten (3)

Image Analyst
Image Analyst am 18 Jan. 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 am 18 Jan. 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 Kommentar
melih guneri
melih guneri am 19 Jan. 2024
Thank you Steven,
I'll try these solution.

Melden Sie sich an, um zu kommentieren.


Hassaan
Hassaan am 18 Jan. 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 Kommentare
melih guneri
melih guneri am 19 Jan. 2024
Thank you Voss

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Shifting and Sorting Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by