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

How do I save a '.txt' file as a '.mat' file, using either MATLAB or Python? I tried using textscan() (in MATLAB), and scipy.io.savemat() (in Python). Both didn't help.

My text file is of the format: value1,value2,value3,valu4 (each row) and has over 1000 rows.

Appreciate any help is appreciated. Thanks in advance.

2 answers

  • answered 2018-11-08 08:16 user7431005

    You can use textscan to read the file and save to save the variables into a .mat file

    fid = fopen('yourTextFile.txt');
    C = textscan(fid,'%f %f %f %f');
    fclose(fid);
    % maybe change the cells from `C` to a single matrix
    M = cell2mat(C);
    save('myMatFile.mat','M');
    

    This works because your file seems to have a fixed format. Have a look at this and this

  • answered 2018-11-08 08:21 liaofeng

    if what you need is to change file format: mv example.mat example.txt