Exporting non-rectangular cell array to delimited text file or excel spreadsheet
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi everyone,
I have a non-rectangular cell array in which I store data for a number of subjects across a number of conditions that I would like to export to excel (it will eventually be going to SPSS, but we are using excel as an intermediary, any advice on improving that is also welcome). The cell array currently looks like this
C =
{1x1 cell } {1x1 cell } {1x1 cell } {1x1 cell }
[6x1 double] [6x1 double] [6x1 double] [5x1 double]
Where C(1,:) = Column headers 'condition1', 'condition2' 'condition3', 'condition4'
and C(2,:) = data 'sub1-cond1', 'sub1-cond2' etc..
ideally I want to put the data for the rest of my subjects in this cell array such that C(2:n+1,:) = data for subjects 1:n. I would then liek to export this to some file so that I can import it into excel, ideally with some delimiter between each subject row so I can keep those seperate. The conditions will not always have the same number of data points for each subject, and cond4 will not always be the short condition, so there seems to be no way to store this data in a form more easily exportable.
Can anyone give me some advice on how I can export a cell array of this form (or something similar) so that I get my data in 4 columns with headers given by C(1,:) and my data given by C(2:n+1),:)? Any help would be much appreciated.
thanks, Jared
댓글 수: 3
Jan
2013년 8월 27일
@Jared: This means, you have e.g. this input:
C = {'Col1', 'Col2', 'Col3'; ...
1:4, 1:5, 1:6};
And you want something like this as output:
['Col1\t\t\tCol2\t\t\t\tCol3\t\t\t\t\t\n', ...
'1\t2\t3\t4\t1\t2\t3\t4\t5\t1\t2\t3\t4\t5\t6\t\n']
Does this match your needs?
답변 (1개)
Azzi Abdelmalek
2013년 8월 26일
% Example
a1=randi(9,6,1);
a2=randi(9,6,1);
a3=randi(9,6,1);
a4=randi(9,5,1);
c = {{'h1'} {'h2'} {'h3'} {'h4'};a1 a2 a3 a4}
%--------------------------------------------------
he=cellfun(@(x) x{:},c(1,:),'un',0);
val=c(2,:);
max_size=max(cellfun(@numel,val));
n_val=cell(6,numel(he));
for k=1:numel(he)
q=cell2mat(val(:,k));
n_val(1:numel(q),k)=num2cell(q);
end
new_cell=[he;n_val]
댓글 수: 3
Azzi Abdelmalek
2013년 8월 26일
you have 4 vectors (3 are 6x1 and one is 5x1). To create one cell array 6x6, we have to add an empty ([]) cell to the last vector. That' what the code do. To export the result to an Excell file
xlswrite('yourfilename.xls',new_cell)
참고 항목
카테고리
Help Center 및 File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!