1

I have, D is a cell array of mismatched cell arrays of strings

A = 

{'abc'; 

'acd' ;

'aaa'}


B = 

{'baa' ;

'bbb'}


C = 

{'acc';

'aaa';

'aad'}

D = { {A}, {B}, {C}}

A, B and C are of different in size and I do not know their size in the first place, so I combined them into D. How do i convert D into a single cell array so that i can write them into an excel sheet?

i.e.

D = 
{'abc' 'baa' 'acc';

'acd' 'bbb' 'aaa';

'aaa' ' '   'aad'}
2
  • So in the desired output, supposing we start writing D to A1 in Excel, A1 would be 'abc' and B3 would be ' '? Commented May 14, 2015 at 12:28
  • Thank you for your reply. B3 would be empty. Commented May 14, 2015 at 12:32

2 Answers 2

1

You can let MATLAB expand the cell array when needed:

F={}; 
for k = 1:numel(D), 
    F(1:numel(D{k}), end+1)=D{k}; 
end

which results in

F = 
    'abc'    'baa'    'acc'
    'acd'    'bbb'    'aaa'
    'aaa'       []    'aad'

If it is important to have empty matrices as empty strings, use

>> F(cellfun('isempty', F))={''}
F = 
    'abc'    'baa'    'acc'
    'acd'    'bbb'    'aaa'
    'aaa'    ''       'aad'
Sign up to request clarification or add additional context in comments.

Comments

0

This would be a vectorized approach -

D = { {A}, {B}, {C}} %// Code from question (not tinkering with it)

%// Extract all cells data at each cell index position. 
%// In other words, reduce level of cell data storage by one step.
D1 = vertcat(D{:})

%// Get lengths of each cell
lens = cellfun('length',D1)

%// Initialize output cell array
out_cell = cell(max(lens),numel(lens))

%// Mask of valid cell positions in the output
mask = bsxfun(@le,[1:max(lens)]',lens(:).')

%// Store concatenated cell data into masked positions in output
out_cell(mask) = vertcat(D1{:})

Sample run -

>> A,B,C
A = 
    'abc'
    'acd'
    'aaa'
B = 
    'baa'
    'bbb'
C = 
    'acc'
    'aaa'
    'aad'
>> out_cell
out_cell = 
    'abc'    'baa'    'acc'
    'acd'    'bbb'    'aaa'
    'aaa'       []    'aad'

Please note that you could avoid creating D altogether, if you do this at the start -

D1 = {A,B,C}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.