How to convert a .mat file into a .csv file?

1,531 ビュー (過去 30 日間)
hello_world
hello_world 2015 年 3 月 30 日
編集済み: Image Analyst 2024 年 7 月 25 日
Hello Friends,
I have a .mat file loaded in workspace. I want to convert it into .csv file. It has real entries. I used to convert it before using the following commands without any problem:
M = dlmread('FileName.mat', '\t', 1, 0);
csvwrite('FileName.csv', M)
But now something is wrong with MATLAB. My dataset is the same. Nothing has changed, but now it gives the following error:
Error using dlmread (line 139)
Mismatch between file and format string.
Trouble reading number from file (row 1u, field 1u) ==>
}Ûa&Ír!ÃþxbSoÈñæo8k:¬i’³*Y#~!IjË9B¯ÝWG~
¬zN=å0ÉÀo8Ó'Rå@õR[iãY>,xÊ8UrVÀ9ó";~~ÀOWAe$Æ\:xfnFVAÿñϼÓЪÍðaþ\n
I wonder if MATLAB changed something with dlmwrite function! Please advise.
  4 件のコメント
Jose Ocampo
Jose Ocampo 2023 年 3 月 1 日
how I can said the link where will be created the file
Image Analyst
Image Analyst 2023 年 3 月 1 日
@Jose Ocampo I don't know what you mean. When you do
csvwrite('FileName.csv', M)
the file is created in the current directory -- what you see in the directory/address field in MATLAB.

サインインしてコメントする。

採用された回答

Jan
Jan 2015 年 3 月 30 日
FileData = load('FileName.mat');
csvwrite('FileName.csv', FileData.M);
  17 件のコメント
Stephen23
Stephen23 2024 年 7 月 25 日
mydata = load('wind.mat');
csvwrite('mydata.csv', mydata);
Incorrect number or types of inputs or outputs for function real.

Error in csvwrite (line 48)
throw(e)
% not in general it doesn't ;)
Image Analyst
Image Analyst 2024 年 7 月 25 日
編集済み: Image Analyst 2024 年 7 月 25 日
@Parisa, your mydata is a structure so you'd have to extract the matrix from the structure first since csvwrite does not take structures as inputs. Something like
% Read variables into fields of a structure.
storedStructure = load('FileName.mat');
% Extract matrix called Data from the structure:
myMatrix = storedStructure.Data; % Change names to whatever you actually have.
% Write the extracted matrix out to a CSV file.
fullFileName = fullfile(pwd, 'MyData.csv'); % Get full path and name.
csvwrite(fullFileName, myMatrix); % Write matrix out to disk.

サインインしてコメントする。

その他の回答 (3 件)

Ashlesh Sortee
Ashlesh Sortee 2017 年 7 月 4 日
simply you can copy and paste the matrix you want in excel
  • all you have to do is open .mat file in matlab , press 'ctr+A' & 'ctr+C' in the variable tab and paste it in new excel sheet
  2 件のコメント
Ayman
Ayman 2024 年 6 月 22 日
but what if the data set is very large it will not work then
Image Analyst
Image Analyst 2024 年 6 月 22 日
@Ayman once you have the variable in MATLAB, then you can use writematrix to write it to a disk file, then use File/Open in Excel to read it into Excel.

サインインしてコメントする。


Ashlesh Sortee
Ashlesh Sortee 2017 年 7 月 4 日
編集済み: Ashlesh Sortee 2017 年 7 月 4 日
FileData = load('FileName.mat'); csvwrite('FileName.csv', FileData.FileName);
  • | | * This one worked for me||*
  • Thanks Jan Simon
  1 件のコメント
YASSINE
YASSINE 2023 年 4 月 25 日
it does not work

サインインしてコメントする。


Image Analyst
Image Analyst 2015 年 3 月 30 日
You're trying to save the variable called FileName20.mat in dlmwrite(). If that's the variable, then you have a dot in the name which means that FileName20 must be a structure, and mat must be a field/member of the structure. It's clearly not - you don't have any structure variable in your program called FileName20. You probably need to pass in M instead of FileName20.
  2 件のコメント
Jan
Jan 2015 年 3 月 30 日
I do not believe that this has worked before. You cannot read a MAT file by dlmread. MAT files are imported by load.
Image Analyst
Image Analyst 2015 年 3 月 30 日
I totally agree with Jan. See Jan's answer, then "Accept" it. It loads your file into a structure called FileData. The "M" array that you stored in the mat file will be a "field" of FileData. Then he writes out only that field to your new csv file.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeText Files についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by