Import data from txt-file with writematrix
8 ビュー (過去 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!