How to save the variable from the workspace into csv or excel file

Hi,
I am using the code below to save one of the variable from the workspace into the
excel file but struggling to save it, what it is doing is saving some gibrish into excel rather then the data.
Code:
close all; clear all; clc;
folder = fullfile('D:','Velocity_Analysis','paraViewpractise');
files = dir( fullfile(folder, '*.csv') );
for ii = 1:length(files)
data = readmatrix(fullfile(files(ii).folder,files(ii).name), 'NumHeaderLines', 1)
res(:,ii)= data(:,3);
end
filename='workspace_variable.xls';
save(filename,'res');
winopen('workspace_variable.xls');

 採用された回答

Ameer Hamza
Ameer Hamza 2020 年 12 月 2 日
編集済み: Ameer Hamza 2020 年 12 月 2 日

1 投票

save() is for saving in a .mat file. You need writematrix() for xls
filename='workspace_variable.xls';
writematrix(res, filename);
For releases older than R2019a, use xlswrite().

8 件のコメント

muhammad choudhry
muhammad choudhry 2020 年 12 月 2 日
hi Hamza,
Please see the screen shot, it is again saving rubbish writting rather then real numbers.
Ameer Hamza
Ameer Hamza 2020 年 12 月 2 日
編集済み: Ameer Hamza 2020 年 12 月 2 日
Can you see the correct values inside your 'res' matrix in MATLAB?
muhammad choudhry
muhammad choudhry 2020 年 12 月 2 日
Please see the screenshots.
Ameer Hamza
Ameer Hamza 2020 年 12 月 2 日
Are you saving the file as csv or xls? How are you opening it in the MS Excel? Can you show your code?
muhammad choudhry
muhammad choudhry 2020 年 12 月 2 日
I just copied the variable from the work space and pasted into excel manually like cut and paste for myself to work with at a moment but would like to have a sort of code which will save the csv, excel or save the data itself as I will need to extract more data from simulations.Didn't write anycode
Ameer Hamza
Ameer Hamza 2020 年 12 月 2 日
This is strange. Following code should work fine
filename='workspace_variable.xls';
writematrix(res, filename);
Which MATLAB release are you using?
muhammad choudhry
muhammad choudhry 2020 年 12 月 2 日
R2020a, yea should work, as all the FAQs answers same as yours and I have tried different ways, I ended up doing it manually. Thanks anyway! I bother you with something else if i stuck again. Thanks for your support!
Ameer Hamza
Ameer Hamza 2020 年 12 月 2 日
You may consider attaching your complete code including the line writematrix. Maybe it will give some hints of the issue.

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

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by