How to save a text file as a .mat file?

90 views (last 30 days)
CuriousThinker
CuriousThinker on 8 Nov 2018
Commented: Stephen23 on 10 Nov 2018
Hi,
I have a text file having data with each row having the format: a1,a2,a3,a4. I have close 2k such rows. I need to save the full text file as a .mat file. How do I get that? I already tried tetxscan but I run into cell within cell issue. Appreciate help.

Answers (4)

Stephen23
Stephen23 on 9 Nov 2018
M = csvread('oldfile.txt');
save('newfile.mat','M')

KSSV
KSSV on 8 Nov 2018
Edited: KSSV on 8 Nov 2018
data = importdata('myfile) ;
save data.mat -v7.3
Using textscan
fid = fopen(myfile) ;
S = textscan(fid,'%f %f %f %f') ;
fclose(fid) ;
a1 = S{1} ; a2 = S{2} ; a3 = S{3} ; a4 = S{4} ;
save data.mat -v7.3 ;
  4 Comments
CuriousThinker
CuriousThinker on 8 Nov 2018
Hi, This the data the data on the file:
0,25.6,100.3,45.7,32.5
10,35.6,200.3,55.7,42.5
20,45.6,300.3,55.7,52.5
30,55.6,400.3,65.7,62.5
40,65.6,500.3,75.7,72.5
50,75.6,600.3,85.7,82.5
60,85.6,700.3,95.7,92.5
70,25.6,100.3,45.7,32.5
80,25.6,100.3,95.7,82.5
90,85.6,100.3,45.7,32.5
100,25.6,200.3,35.7,32.5
When I run the above code, and try
load('data.mat')
I'm getting only the first row.
KSSV
KSSV on 9 Nov 2018
data = importdata('data.txt') ; % you can use load also
save data.mat -v7.3
It is giving you the full data in text file.

Sign in to comment.


madhan ravi
madhan ravi on 8 Nov 2018
Matrix=load('sample.txt') %assuming text file is numeric
save sample.mat Matrix

CuriousThinker
CuriousThinker on 9 Nov 2018
Edited: CuriousThinker on 9 Nov 2018
Hi,
Thank you for the suggestions. I was able to solve it using csvread()
data = importdata('myfile') ;
save('data_mat.mat','data');
  1 Comment
Stephen23
Stephen23 on 10 Nov 2018
"I was able to solve it using csvread()"
Don't forget to accept the answer that showed you how to use csvread.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!