How to save output of variable in csv file?

조회 수: 693 (최근 30일)
tejasvee
tejasvee 2017년 6월 17일
댓글: Star Strider 2023년 5월 4일
I have MATLAB code and output is saved in variable and i want to save in csv file. And there is no example of that in doc of MATLAB. I have attached the code and variable name is"TY".

채택된 답변

Star Strider
Star Strider 2017년 6월 17일
See the documentation on the csvwrite (link) function. It was introduced prior to R2006a, so you should have access to it. There are similar functions linked to in and at the end of that documentation page.
  댓글 수: 1
Star Strider
Star Strider 2023년 5월 4일
The writematrix function was introduced in R2019a, two years after I posted this.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Md. Al-Imran Abir
Md. Al-Imran Abir 2022년 7월 4일
At present, csvwrite (documentation) is not recommended. If one has a newer version of MATLAB (2019a or later), writematrix (documentation) is the preferred way.

Erika Joh
Erika Joh 2019년 4월 26일
편집: Erika Joh 2019년 4월 26일
Thanks! Would I just call on the function several times then, like:
csvwrite('filename.csv',variable1,0,1)
csvwrite('filename.csv',variable2,0,2)
etc?
  댓글 수: 4
Shekhar Vats
Shekhar Vats 2021년 4월 28일
This is a stupid bruteforce method, if you find a better one please share. I have seen people just pointing to the default matlab way where you can save one variable. What if you have 1000's of variable that will take all the time. I hope someone will share a better code
for downloading xlscol function
clear all; close all; clc;
current_dir = pwd;
[Filename, Pathname] = uigetfile('Select File'); % select file
file_to_be_loaded = [Pathname '\' Filename]; % get file path and name
load(file_to_be_loaded); % load file
Filename
clear Pathname file_to_be_loaded current_dir
vars=who;
sheet = 1;
for n=1:size(vars)
n
xlRange = [xlscol(n) '1'];
xl_range = [xlscol(n) '2'];
xlswrite([Filename(1:end-4) '.xlsx'],cell(vars(n)),sheet, xlRange)
xlswrite([Filename(1:end-4) '.xlsx'],eval(char(vars(n))),sheet, xl_range)
end
cillian thomas
cillian thomas 2023년 5월 4일
A better way is to put the variables in a matrix, and export the data. See this thread:
xout1 = [1 2 3]
yout1 = [4 5 6]
m = [xout1,yout1]
csvwrite('filename.csv',m)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by