Matrix insertion to a txt-file

조회 수: 5 (최근 30일)
Robert Bag
Robert Bag 2021년 5월 14일
댓글: Adam Danz 2021년 5월 20일
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
Robert Bag
Robert Bag 2021년 5월 14일
편집: Adam Danz 2021년 5월 14일
This gets me everything but retreiving "back" my information from the txt-file.
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',' ','WriteMode','append')
Y = randi(9,5,7);
txt2 = ['Second matrix dimensions: ' num2str(size(Y,1)) ' by ' num2str(size(Y,2))];
writematrix(txt2,'Mymatrices.txt','delimiter',' ','WriteMode','append')
writematrix(Y,'Mymatrices.txt','delimiter',' ','WriteMode','append')
Z = randi(9,20,2);
txt3 = ['Third matrix dimensions: ' num2str(size(Z,1)) ' by ' num2str(size(Z,2))];
writematrix(txt3,'Mymatrices.txt','delimiter',' ','WriteMode','append')
writematrix(Z,'Mymatrices.txt','delimiter',' ','WriteMode','append')
clear
type('Mymatrices.txt');
save('Matlab283workspace.mat')
Adam Danz
Adam Danz 2021년 5월 20일
@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.

댓글을 달려면 로그인하십시오.

답변 (2개)

Mathieu NOE
Mathieu NOE 2021년 5월 14일
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
Robert Bag
Robert Bag 2021년 5월 14일
But I just wonder then how a I can do that it will write out alll three matrices from the txt-file when using the readmatrix?
Adam Danz
Adam Danz 2021년 5월 14일
+1, simple and effective.

댓글을 달려면 로그인하십시오.


Adam Danz
Adam Danz 2021년 5월 14일
편집: Adam Danz 2021년 5월 14일
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
Robert Bag
Robert Bag 2021년 5월 14일
Couldnt I just use dlmread and "skip" the rows with text in the reading?
Robert Bag
Robert Bag 2021년 5월 14일
And does "append" work with writematrix? Sry for all the questions but I am a beginner ;)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by