printing nxnxn matrix into a file
65 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(3 个)
Image Analyst
2024-1-18
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
2024-1-18
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])
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
data = load('mymatfile.mat')
disp(data.A)
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)
writematrix(S, 'mytextfile.txt')
type mytextfile.txt
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(')
Hassaan
2024-1-18
% 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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!