Import data from txt-file with writematrix
Afficher commentaires plus anciens
I have this code. The only thing I want now is to import the data into three matrices to the workspace. To a mat-file
X = randi(9,3,5);
txt1 = ('Name: X');
writematrix(txt1,'Mymatrices.txt','delimiter',' ');
txt1a = ['Size: ' num2str(size(X,1)) ' x ' num2str(size(X,2))];
writematrix(txt1a,'Mymatrices.txt','delimiter',' ','WriteMode','append');
txt1b = ['Class: ' num2str(class (X))];
writematrix(txt1b,'Mymatrices.txt','delimiter',' ','WriteMode','append');
writematrix(X,'Mymatrices.txt','delimiter','tab','WriteMode','append');
Y = randi(9,5,7);
txt2 = ('Name: Y');
writematrix(txt2,'Mymatrices.txt','delimiter',' ','WriteMode','append');
txt2a = ['Size: ' num2str(size(Y,1)) ' x ' num2str(size(Y,2))];
writematrix(txt2a,'Mymatrices.txt','delimiter',' ','WriteMode','append');
txt2b = ['Class: ' num2str(class (Y))];
writematrix(txt2b,'Mymatrices.txt','delimiter',' ','WriteMode','append');
writematrix(Y,'Mymatrices.txt','delimiter','tab','WriteMode','append');
Z = randi(9,20,2);
txt3 = ('Name: Z');
writematrix(txt3,'Mymatrices.txt','delimiter',' ','WriteMode','append');
txt3a = ['Size: ' num2str(size(Z,1)) ' X ' num2str(size(Z,2))];
writematrix(txt3a,'Mymatrices.txt','delimiter',' ','WriteMode','append');
txt3b = ['Class: ' num2str(class (Z))];
writematrix(txt3b,'Mymatrices.txt','delimiter',' ','WriteMode','append');
writematrix(Z,'Mymatrices.txt','delimiter','tab','WriteMode','append');
clear
importdata('Mymatrices.txt')
save('Matlab283workspace.mat')
Réponses (3)
Cris LaPierre
le 15 Mai 2021
Modifié(e) : Cris LaPierre
le 15 Mai 2021
0 votes
You are not going to be able to use importdata, readmatrix, etc. because the formatting of your file is inconsistant. You are most likely going to have to use an approach that lets you inspect the contents of specific lines throughout the import process.
Are you creating the file yourself, or is this just an example you've put together? If the former, can you share one of your files? You can attach it using the paperclip icon. If the latter, why not create 3 separate files instead?
When the formatting in consistant on every line, look to the Import Text Files page. When it is not, you may need to create a solution using the Low-Level File I/O page.
9 commentaires
Robert Bag
le 15 Mai 2021
Robert Bag
le 15 Mai 2021
Robert Bag
le 15 Mai 2021
Robert Bag
le 15 Mai 2021
Robert Bag
le 15 Mai 2021
Cris LaPierre
le 15 Mai 2021
No one is saying readmatrix doesn't output a matrix. However, readmatrix expects every row to contain the same number of columns, and use the same delimeters. It also expects all headers to be at the top of the file, followed by data.
Your file contains 3 different headers used in 3 different locations in the file. Your first matrix has 5 columns. The second contains 7. The third contains 2.
What is happening is readmatrix is treating everying up to the third matrix as header lines, and is skipping them. Even if you could get it to read the entire file, the output would still be a single matrix, not 3.
A solution would likely have to read in the line indicating the size, and then use that information to import the corresponding matrix, and repeat until all 3 matrices have been read.
Robert Bag
le 15 Mai 2021
Robert Bag
le 15 Mai 2021
Walter Roberson
le 15 Mai 2021
textscan would work better than readmatrix
Robert Bag
le 16 Mai 2021
0 votes
Catégories
En savoir plus sur Text Files dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!