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

23 Ansichten (letzte 30 Tage)
CuriousThinker
CuriousThinker am 8 Nov. 2018
Kommentiert: Walter Roberson am 9 Jan. 2025
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.

Antworten (4)

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

KSSV
KSSV am 8 Nov. 2018
Bearbeitet: KSSV am 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 Kommentare
CuriousThinker
CuriousThinker am 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 am 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.

Melden Sie sich an, um zu kommentieren.


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

CuriousThinker
CuriousThinker am 9 Nov. 2018
Bearbeitet: CuriousThinker am 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');
  3 Kommentare
waleed
waleed am 9 Jan. 2025
Bearbeitet: waleed am 9 Jan. 2025
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
Walter Roberson am 9 Jan. 2025
Be careful, your filename probably does not end in a space.
wifi_motion = importdata('wifi_motion.txt') ;

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by