Exporting non-rectangular cell array to delimited text file or excel spreadsheet

1 view (last 30 days)
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 Comments
Jared Z
Jared Z on 26 Aug 2013
Sorry Jan,
To clarify, the first row are the column headers, I think I had those stored in the 1x1 cells, but I've since changed to so that the first row is a series of 4 strings which are to server as the column headers, and each subsequent row corresponds to one subject's data for each of the 4 conditions. Each subject will have somewhere between 4 and 6 data points per condition.
Jan
Jan on 27 Aug 2013
@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?

Sign in to comment.

Answers (1)

Azzi Abdelmalek
Azzi Abdelmalek on 26 Aug 2013
% 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 Comments
Azzi Abdelmalek
Azzi Abdelmalek on 26 Aug 2013
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)
Jared Z
Jared Z on 26 Aug 2013
Hi Azzi,
Thank you, but unfortunately this does not help me. I am going to need to add data to my current cell array. Right now, C(2,:) is the data for the 4 conditions for subject 1. I will be adding to the cell array more rows such that C(2:n-1,:) will be data for subjects 1:n where n= my number of subjects. Each subject will have data for the 4 conditions,,\ but the data vectors will not always be the same length for each condition, and the short ones will not always fall in the same column, so a need a robust method for exporting my cell array when the size of the cells is changing. It would also be ideal if I could keep the structure of the cell array intact so that the divisions between each subjects data are obvious in the exported spread sheet. It seems like this may be difficult to do though.

Sign in to comment.

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!