MATLAB Answers

Matrix insertion to a txt-file

1 view (last 30 days)
Robert Bag
Robert Bag on 14 May 2021
Commented: Adam Danz on 20 May 2021
I am trying to get matlab to put three matrices into a text file, in a nice fashion.
I have this code:X = randi(10,3,5);
dlmwrite('Mymatrices.txt',X)
Y = randi(10,5,7);
dlmwrite('Mymatrices.txt',Y,'-append')
Z = randi(10,20,2);
dlmwrite('Mymatrices.txt',Z,'-append')
clear
%dlmwrite'Mymatrices.txt')
readmatrix('Mymatrices.txt')
save('Matlab283workspace.mat')
BUT I would like to modify it to :
  1. Put the matrices in a nicer fashion in the text file, and without the commas.
  2. Let each matrix be preceeded by the information:size, above the matrices , in the text-file
  3. When i write "readmatrix" at the end, it only writes out the last matrix - allthough all three matrices are present in the txt-file
Thanks for input!
  10 Comments
Adam Danz
Adam Danz on 20 May 2021
@Robert Bag you've got 13 questions, all of which have at least one answer, but you've never accepted any of them. Your questions are interesting and it looks like you've received a lot of help. Please take the time to accept answers that pointed you in the right direction. See link.

Sign in to comment.

Answers (2)

Mathieu NOE
Mathieu NOE on 14 May 2021
hello Robert
try this :
clc
clearvars
dlmwrite('Mymatrices.txt','----------------------','delimiter','')
X = randi(10,3,5);
txt1 = [' First matrix dimensions : ' num2str(size(X,1)) ' by ' num2str(size(X,2))];
dlmwrite('Mymatrices.txt',txt1,'delimiter','','-append')
dlmwrite('Mymatrices.txt',X,'delimiter','\t','-append')
dlmwrite('Mymatrices.txt','----------------------','delimiter','','-append')
Y = randi(10,5,7);
txt2 = [' Second matrix dimensions : ' num2str(size(Y,1)) ' by ' num2str(size(Y,2))];
dlmwrite('Mymatrices.txt',txt2,'delimiter','','-append')
dlmwrite('Mymatrices.txt',Y,'delimiter','\t','-append')
dlmwrite('Mymatrices.txt','----------------------','delimiter','','-append')
Z = randi(10,20,2);
txt3 = [' Third matrix dimensions : ' num2str(size(Z,1)) ' by ' num2str(size(Z,2))];
dlmwrite('Mymatrices.txt',txt3,'delimiter','','-append')
dlmwrite('Mymatrices.txt',Z,'delimiter','\t','-append')
dlmwrite('Mymatrices.txt','----------------------','delimiter','','-append')
clear
%dlmwrite'Mymatrices.txt')
readmatrix('Mymatrices.txt')
save('Matlab283workspace.mat')
will give this output :
----------------------
First matrix dimensions : 3 by 5
10 10 8 9 8
1 10 1 9 4
10 7 2 2 3
----------------------
Second matrix dimensions : 5 by 7
9 10 10 7 6 9 1
5 4 4 3 9 3 6
7 6 7 6 10 10 5
1 8 5 2 8 10 1
8 2 7 10 6 10 10
----------------------
Third matrix dimensions : 20 by 2
8 3
1 10
8 2
3 9
10 5
2 8
6 4
5 9
5 9
9 2
3 7
6 2
10 5
7 6
1 6
4 8
9 8
4 10
8 5
8 5
----------------------
  3 Comments
Adam Danz
Adam Danz on 14 May 2021
+1, simple and effective.

Sign in to comment.


Adam Danz
Adam Danz on 14 May 2021
Edited: Adam Danz on 14 May 2021
Another way to do this is by using formattedDisplayText() which became available in Matlab R2021a (see Community Highlight).
Collect all of the text in a string array.
The string array will be vertically concatenated.
X = randi(10,3,5);
Y = randi(10,5,7);
Z = randi(10,20,2);
clear('txt')
txt(1) = "----------------------";
txt(2) = string(sprintf(' First matrix dimensions : %d by %d', size(X)));
txt(3) = formattedDisplayText(X);
txt(4) = txt(1);
txt(5) = string(sprintf(' Second matrix dimensions : %d by %d', size(Y)));
txt(6) = formattedDisplayText(Y);
txt(7) = txt(1);
txt(8) = string(sprintf(' Third matrix dimensions : %d by %d', size(Y)));
txt(9) = formattedDisplayText(Z);
txt(10) = txt(1);
Join the string array into a single string
% Remove any new-line characters from the formattedDisplayText outputs
txt = regexprep(txt,'\n$', '');
% Join string array into single string
txtCat = strjoin(txt(:),newline)
txtCat =
"---------------------- First matrix dimensions : 3 by 5 9 8 9 1 7 7 9 7 6 1 7 10 5 9 10 ---------------------- Second matrix dimensions : 5 by 7 1 5 7 2 9 8 8 4 3 9 2 8 9 6 2 7 3 1 9 8 5 7 4 3 3 4 5 3 4 4 10 1 9 3 10 ---------------------- Third matrix dimensions : 5 by 7 8 10 1 8 10 5 10 5 10 7 1 4 1 10 8 7 4 3 1 7 1 2 6 7 5 1 4 2 7 8 9 5 2 7 10 4 4 2 1 4 ----------------------"
Write string to text file
This example writes to the temporary directory. Replace tempdir with another directory path if you don't want to save to the temp dir.
% Write to text file
file = fullfile(tempdir,'Mymatrices.txt');
fid = fopen(file,'w+');
cleanup = onCleanup(@()fclose(fid));
fprintf(fid, '%s', txtCat);
clear cleanup
winopen(file) % Opens the text file, for Windows platforms
  5 Comments
Robert Bag
Robert Bag on 14 May 2021
And does "append" work with writematrix? Sry for all the questions but I am a beginner ;)

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!