Matrix insertion to a txt-file

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 Commenti

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')
Adam Danz
Adam Danz il 14 Mag 2021
Modificato: Adam Danz il 14 Mag 2021
'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.
ok thanx bro
Super it worked! thnx
Again the problem. Everthing works without retreiving "back" the same information I put in to the txt-file.
I rund readmatrix(file) but it always only retreives and stores the values from the last matrix in the workspace
I fixed it using type command
Adam Danz
Adam Danz il 14 Mag 2021
Are you following the example in the documentation?
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].
Yes. I got that. Now, I have problem loading back the matrices into the workspace from the txt-file.
Type will get me the txtfile in the command window but I dont retreive the matrices(X,Y and so on)..
Robert Bag
Robert Bag il 14 Mag 2021
Modificato: Adam Danz il 14 Mag 2021
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 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.

Accedi per commentare.

Risposte (2)

Mathieu NOE
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

wow. Thanks a lot. I sure will try this
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 il 14 Mag 2021
+1, simple and effective.

Accedi per commentare.

Adam Danz
Adam Danz il 14 Mag 2021
Modificato: Adam Danz il 14 Mag 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 Commenti

Thanx guys. Now all that is left is to retreive the information in the txt-file to the workspace and save it as the three matrices.
Adam Danz
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.
thanx. I Did writematrix and readmatrix first. But that I werent able to make it work with delimiters and so on and i didnt get to find any information about it in Mathworks.
Couldnt I just use dlmread and "skip" the rows with text in the reading?
And does "append" work with writematrix? Sry for all the questions but I am a beginner ;)

Accedi per commentare.

Categorie

Richiesto:

il 14 Mag 2021

Commentato:

il 20 Mag 2021

Community Treasure Hunt

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

Start Hunting!

Translated by