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

102 ビュー (過去 30 日間)
Srika
Srika 2016 年 4 月 6 日
コメント済み: Ola Fekry 2022 年 2 月 19 日
I have a .mat file loaded in workspace. I want to convert it into .csv file.
FileData = load('Trainset.mat');
csvwrite('FileName.csv', FileData.M);
I used the above method to convert but i get this error. >> csv_Con Reference to non-existent field 'M'.
Error in csv_Con (line 2) csvwrite('FileName.csv', FileData.M);
>> >> >>
  1 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2016 年 4 月 6 日
What is FileData.M? its size?

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

採用された回答

Walter Roberson
Walter Roberson 2016 年 4 月 6 日
FileData = load('Trainset.mat');
fields = fieldnames(FileData);
for K = 1 : length(fields)
thisvar = fields{K};
thisdata = FileData.(thisvar);
if ~isnumeric(thisdata)
warning('Skipping field %s which is type %s instead of numeric', thisvar, class(thisvar));
else
thisfile = sprintf('FileName_%s.csv', thisvar);
csvwrite(thisfile, thisdata);
end
end
  1 件のコメント
Ola Fekry
Ola Fekry 2022 年 2 月 19 日
Plz I have a RF signal dataset of 220000 .mat file how can I put them in CSV file to be used in classification learner app for evaluation?

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

その他の回答 (2 件)

Azzi Abdelmalek
Azzi Abdelmalek 2016 年 4 月 6 日
FileData = load('Trainset.mat');
v=reshape([FileData.M],size(FileData))
csvwrite('FileName.csv', v)

MBD4FUN
MBD4FUN 2016 年 4 月 6 日
Don't know if this is what you are looking for but here is one way of solving this problem.
FileData = load('Trainset.mat');
% get all varaible names
varNameList = fields(FileData);
% create a csv file to dump all data
fid = fopen('Trainset.csv','wt')
% write header if you prefer
fprintf(fid,'ColumnA,ColumnB');
for i = 1:length(varNameList)
switch lower(FileData.(varNameList{1}).class)
case 'char'
% write data to CSV file
fprintf(fid,'%s,%s\n');
case 'double'
% do whatever you want with the double
% ...and write data to CSV file
fprintf(fid,'%s,%s\n');
% ...
end
end
% close CSV file
fclose(fid);
BTW, you might want to consider wrap up this block of code in a function ;-)
Happy scripting,
MBD4FUN

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by