Import data from txt-file with writematrix

4 views (last 30 days)
Robert Bag
Robert Bag on 15 May 2021
Answered: Robert Bag on 16 May 2021
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')

Answers (3)

Cris LaPierre
Cris LaPierre on 15 May 2021
Edited: Cris LaPierre on 15 May 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 Comments
Robert Bag
Robert Bag on 15 May 2021
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.
Walter Roberson
Walter Roberson on 15 May 2021
textscan would work better than readmatrix

Sign in to comment.


Robert Bag
Robert Bag on 15 May 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 Comment
Robert Bag
Robert Bag on 15 May 2021
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)

Sign in to comment.


Robert Bag
Robert Bag on 16 May 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')

Community Treasure Hunt

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

Start Hunting!