Import data from txt-file with writematrix
4 次查看(过去 30 天)
显示 更早的评论
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')
0 个评论
回答(3 个)
Cris LaPierre
2021-5-15
编辑:Cris LaPierre
2021-5-15
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!