How to save a text file as a .mat file?
Show older comments
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
on 9 Nov 2018
M = csvread('oldfile.txt');
save('newfile.mat','M')
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
on 8 Nov 2018
KSSV
on 8 Nov 2018
The above saves entire data of text file into mat file.
CuriousThinker
on 8 Nov 2018
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.
madhan ravi
on 8 Nov 2018
Matrix=load('sample.txt') %assuming text file is numeric
save sample.mat Matrix
CuriousThinker
on 9 Nov 2018
Edited: CuriousThinker
on 9 Nov 2018
3 Comments
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.
data = importdata('myfile') ;
save('data_mat.mat','data');
to use this command: my file name is wifi_motion.txt
wifi_motion = importdata('wifi_motion.txt ') ;
save('wifi_motion_mat.mat','wifi_motion');
Walter Roberson
on 9 Jan 2025
Be careful, your filename probably does not end in a space.
wifi_motion = importdata('wifi_motion.txt') ;
Categories
Find more on Text Data Preparation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!