Matrix insertion to a txt-file
11 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Adam Danz
le 20 Mai 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.
Réponses (2)
Mathieu NOE
le 14 Mai 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 commentaires
Adam Danz
le 14 Mai 2021
Modifié(e) : Adam Danz
le 14 Mai 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)
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/617848/image.png)
5 commentaires
Voir également
Catégories
En savoir plus sur Characters and Strings dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!