Matrix insertion to a txt-file
Mostra commenti meno recenti
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 :
- Put the matrices in a nicer fashion in the text file, and without the commas.
- Let each matrix be preceeded by the information:size, above the matrices , in the text-file
- 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 Commenti
Adam Danz
il 14 Mag 2021
Robert Bag's answer moved here as a comment
Whats wtrong with this code_
X = randi(9,3,5);
txt1 = ['First matrix dimensions: ' num2str(size(X,1)) ' by ' num2str(size(X,2))];
writematrix(txt1,'Mymatrices.txt','delimiter', ' ')
writematrix(X,'Mymatrices.txt','delimiter',' ','append')
Y = randi(9,5,7);
txt2 = ['Second matrix dimensions: ' num2str(size(Y,1)) ' by ' num2str(size(Y,2))];
writematrix(txt2,'Mymatrices.txt','delimiter',' ', 'append')
writematrix(Y,'Mymatrices.txt','delimiter',' ', 'append')
Z = randi(9,20,2);
txt3 = ['Third matrix dimensions: ' num2str(size(Z,1)) ' by ' num2str(size(Z,2))];
writematrix(txt3,'Mymatrices.txt','delimiter',' ', 'append')
writematrix(Z,'Mymatrices.txt','delimiter',' ','append')
clear
%dlmread('Mymatrices.txt')
save('Matlab283workspace.mat')
I get this:
Error using writematrix (line 191)
Wrong number of arguments. A filename must be provided when supplying additional parameters, and each
parameter name must be followed by a value.
Error in Matlbabwritematrix281 (line 4)
writematrix(X,'Mymatrices.txt','delimiter',' ','append')
'append' is part of the name-value pair 'WriteMode','append'.
You forgot the name-part.
writematrix(X,'Mymatrices.txt','delimiter',' ','WriteMode','append')
Notice this part of the error message
Wrong number of arguments. A filename must be provided when supplying additional parameters, and each parameter name must be followed by a value.
Robert Bag
il 14 Mag 2021
Robert Bag
il 14 Mag 2021
Robert Bag
il 14 Mag 2021
Robert Bag
il 14 Mag 2021
Adam Danz
il 14 Mag 2021
If you write matrix X and then append the file with matrix Y with the same number of columns, when you read in that file you'll get a single matrix [X;Y].
Robert Bag
il 14 Mag 2021
Robert Bag
il 14 Mag 2021
Modificato: Adam Danz
il 14 Mag 2021
Adam Danz
il 20 Mag 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.
Risposte (2)
Mathieu NOE
il 14 Mag 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 Commenti
Robert Bag
il 14 Mag 2021
Robert Bag
il 14 Mag 2021
Adam Danz
il 14 Mag 2021
+1, simple and effective.
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)
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 Commenti
Robert Bag
il 14 Mag 2021
Adam Danz
il 14 Mag 2021
If that's the goal, then creating a formatted text file makes the task harder. There's a difference between human-readable and computer-readable formats.
The best way to write matrices to a text file so that a computer can read the file is by using writematrix and readmatrix.
Robert Bag
il 14 Mag 2021
Robert Bag
il 14 Mag 2021
Robert Bag
il 14 Mag 2021
Categorie
Scopri di più su Characters and Strings in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!