Import data from txt-file with writematrix

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')

Risposte (3)

Cris LaPierre
Cris LaPierre il 15 Mag 2021
Modificato: Cris LaPierre il 15 Mag 2021
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 Commenti

I give you the file in question
All three must be in the same tx-file
In what way are the formatting inconsistent?
When I run readmatrix the it creates only the last matrix (from the three).
Readmatrix clearly outputs a matrix but I want it to extract all three. I thought there was some option creating multiple matrices from the same file.
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.
thnx for info
Here is actually what I am trying to do.
Write a script that prints three random matrices (3× 5, 5 × 7, 20 × 2) into a text file. Each matrix in the text file should be preceded by several header lines containing information about the matrix, for example, size etc. The script then clears the workspace and loads the matrices from the text file to the workspace. The workspace containing three matrices is then saved to the mat-file.
textscan would work better than readmatrix

Accedi per commentare.

Robert Bag
Robert Bag il 15 Mag 2021
Yes, Walter. I am on it right now. How sharp are you on the subject?
I do not get it to work. It seems hard to get the three matrices to be stored in three variables.

1 Commento

This gives me nothing so far:
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
%fid = fopen('Mymatrices.txt')
%data1 = textscan(fid, '%f%d%d',2)
%fileID = fopen('Mymatrices.txt','%f');
%fclose(fileID);
fp = fopen('Mymatrices.txt','r')
A = fscanf(fp,'%f',[3,5])
%fp = fopen('Mymatrices.txt','r');
%s = fscanf(fp,'%s%',9);
%v = fscanf(fp,'%f%',90);
fclose(fp)

Accedi per commentare.

Robert Bag
Robert Bag il 16 Mag 2021
This became the final code that worked my purposes.
%creating my text-file
X = randi(9,3,5);
txt1 = ('Name:X');
writematrix(txt1,'Mymatrices.txt');
txt1a = ['Size: ' num2str(size(X,1)) 'x' num2str(size(X,2))];
writematrix(txt1a,'Mymatrices.txt','WriteMode','append');
txt1b = ['Class: ' num2str(class (X))];
writematrix(txt1b,'Mymatrices.txt','WriteMode','append');
writematrix(X,'Mymatrices.txt','delimiter','space','WriteMode','append');
Y = randi(9,5,7);
txt2 = ('Name: Y');
writematrix(txt2,'Mymatrices.txt','WriteMode','append');
txt2a = ['Size: ' num2str(size(Y,1)) ' x ' num2str(size(Y,2))];
writematrix(txt2a,'Mymatrices.txt','WriteMode','append');
txt2b = ['Class: ' num2str(class (Y))];
writematrix(txt2b,'Mymatrices.txt','WriteMode','append');
writematrix(Y,'Mymatrices.txt','delimiter','space','WriteMode','append');
Z = randi(9,20,2);
txt3 = ('Name: Z');
writematrix(txt3,'Mymatrices.txt','WriteMode','append');
txt3a = ['Size: ' num2str(size(Z,1)) ' X ' num2str(size(Z,2))];
writematrix(txt3a,'Mymatrices.txt','WriteMode','append');
txt3b = ['Class: ' num2str(class (Z))];
writematrix(txt3b,'Mymatrices.txt','WriteMode','append');
writematrix(Z,'Mymatrices.txt','delimiter','space','WriteMode','append');
clear %clears all workspace
%code for retreiving the matrix X from txt-file
opts = delimitedTextImportOptions("NumVariables", 5);
opts.DataLines = [4, 6];
opts.Delimiter = " ";
opts.VariableNames = ["Name", "Z", "VarName3", "VarName4", "VarName5"];
opts.VariableTypes = ["double", "double", "double", "double", "double"];
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
opts.ConsecutiveDelimitersRule = "join";
opts.LeadingDelimitersRule = "ignore";
opts = setvaropts(opts, ["VarName3", "VarName5"], "TrimNonNumeric", true);
opts = setvaropts(opts, ["VarName3", "VarName5"], "ThousandsSeparator", ",");
X = readtable("C:\Users\ruber\OneDrive\Mymatrices.txt", opts);
X = table2array(X);
%clear opts
%code for retreiving the matrix Y from txtfile
opts = delimitedTextImportOptions("NumVariables", 7);
opts.DataLines = [10, 14];
opts.Delimiter = " ";
opts.VariableNames = ["Name", "Z", "VarName3", "VarName4", "VarName5", "VarName6", "VarName7"];
opts.VariableTypes = ["double", "double", "double", "double", "double", "double", "double"];
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
opts.ConsecutiveDelimitersRule = "join";
opts.LeadingDelimitersRule = "ignore";
opts = setvaropts(opts, ["VarName3", "VarName5", "VarName6", "VarName7"], "TrimNonNumeric", true);
opts = setvaropts(opts, ["VarName3", "VarName5", "VarName6", "VarName7"], "ThousandsSeparator", ",");
Y = readtable("C:\Users\ruber\OneDrive\Mymatrices.txt", opts);
Y = table2array(Y);
clear opts
%code for retreiving the matrix Z from txt-file
opts = delimitedTextImportOptions("NumVariables", 4);
opts.DataLines = [18, Inf];
opts.Delimiter = " ";
opts.VariableNames = ["Name", "Z", "Var3", "Var4"];
opts.SelectedVariableNames = ["Name", "Z"];
opts.VariableTypes = ["double", "double", "char", "char"];
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
opts.ConsecutiveDelimitersRule = "join";
opts.LeadingDelimitersRule = "ignore";
opts = setvaropts(opts, ["Var3", "Var4"], "WhitespaceRule", "preserve");
opts = setvaropts(opts, ["Var3", "Var4"], "EmptyFieldRule", "auto");
Z = readtable("C:\Users\ruber\OneDrive\Mymatrices.txt", opts);
Z = table2array(Z);
clear opts
save('Matlabwritematrix281.mat')

Richiesto:

il 15 Mag 2021

Risposto:

il 16 Mag 2021

Community Treasure Hunt

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

Start Hunting!

Translated by