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

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)

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

Thank you very much for the answer. But, this will only get values of a single row. How to save the entire text file to mat?
The above saves entire data of text file into mat file.
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.
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.

Matrix=load('sample.txt') %assuming text file is numeric
save sample.mat Matrix
Hi,
Thank you for the suggestions. I was able to solve it using csvread()
data = importdata('myfile') ;
save('data_mat.mat','data');

3 Comments

"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');
Be careful, your filename probably does not end in a space.
wifi_motion = importdata('wifi_motion.txt') ;

Sign in to comment.

Categories

Asked:

on 8 Nov 2018

Commented:

on 9 Jan 2025

Community Treasure Hunt

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

Start Hunting!