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!