[英]Exporting data from cell to Excel file
Let's say I have a cell named data
like this: 假设我有一个名为
data
的单元格,如下所示:
data{1} = vector1;
data{2} = vector2;
...
data{n} = vectorn;
All vectors (with numerical values) in data
have the same 1xN size. 在所有的向量(与数值)
data
具有相同的1×N个尺寸。
Now, I want to export this data file into an .xlsx document where each row is a vector and I want to label each column. 现在,我想将此数据文件导出到.xlsx文档中,其中每一行都是矢量,并且我想为每一列加上标签。 The result should be something like this:
结果应该是这样的:
label1 label2 ... labelN
vector1(1,1) vector1(1,2) ... vector1(1,N)
... ... ... ...
vectorn(1,1) vectorn(1,2) ... vectorn(1,N)
I tried to do this using: 我尝试使用以下方法执行此操作:
n=10;
N=5;
for i=1:n
data{i}=rand(1,N);
end
filename='test.xlsx';
xlswrite(filename,data)
but my .xlsx file comes with all the data from data
in just one row. 但是我的.xlsx文件仅将数据中的所有
data
放在一行中。 And I don't know how to do the labels. 而且我不知道如何做标签。
Please help me. 请帮我。
This can be done using vertcat
, num2cell
, sprintf
, strsplit
and xlswrite
as follows: 这可以使用
vertcat
, num2cell
, sprintf
, strsplit
和xlswrite
,如下所示:
modified_data = num2cell(vertcat(data{:})); % Converting 1xn cell into nxN cell
% Generating Column Headers as specified in the question
col_header = strsplit(sprintf('label%d ' , 1:N));
col_header = col_header(1:end-1);
% If N is not much high number (e.g; if N=5), you can input Column Headers as:
% col_header = {'label1','label2','label3','label4','label5'};
filename='test.xlsx'; % Name of the excel file to be written
xlswrite(filename,[col_header; modified_data]); % Writing the excel file
Its because you call rand(1,N)
together in one cell ( data{i}
). 这是因为您在一个单元格(
data{i}
)中一起调用rand(1,N)
)。 For each value in its own cell you have to create a nxN matrix of cells, which is easiest done if you transfrom the entire matirx: 对于它自己的单元格中的每个值,您必须创建一个nxN的单元格矩阵,如果您要转换整个矩阵,则最容易做到:
n=10;
N=5;
data=rand(n,N);
celldata=num2cell(data);
filename='test.xlsx';
xlswrite(filename,celldata);
otherwise you have to make two loops but thats not so great performancewise 否则,您必须进行两个循环,但这并不是那么好的性能
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.