How to convert a large set of variables in the MATLAB workspace into an Excel file?
조회 수: 1 (최근 30일)
이전 댓글 표시
I want to take my Workspace variables and download them as an Excel file. Here's how I am doing it
% Clean your workspace
clear
close
clc
% Create variables
a = "Hello World";
b = "Today is Friday";
c = "I am happy";
% Define to filenames
varsFile = "workspace.csv";
% Convert variables to tables
dataTable = table(a, b, c);
% Write the tables to their respective files
writetable(dataTable, varsFile);
But suppose that have many many variables. How can I do this without calling each single variable. Is there a loop that can do this for me given a large set of variables? Thanks!
댓글 수: 3
Stephen23
2021년 9월 21일
Storing all of your hundreds of arrays in one structure would make your task easy and efficient.
Unfortunately you have lots of separate variables, which makes this task complex and inefficient.
답변 (1개)
Image Analyst
2021년 9월 21일
Try this and adapt as needed:
% Create variables
a = "Hello World";
b = "Today is Friday";
c = "I am happy";
% Define workbook filename.
varsFile = "workspace.csv";
s = whos;
variableNames = {s.name}
for k = 1 : length(variableNames)
% Write the variable to Excel.
% How you do it depends on what the data type is
% and where you want them to go.
end
댓글 수: 4
Image Analyst
2021년 9월 22일
Then just call writecell() inside the loop. I believe each time you call it, it will just blast over the existing workbook, and retain existing values, so make sure you change the sheet or cell location when you do the write so you don't clobber old data with new data.
참고 항목
카테고리
Help Center 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!