MATLAB Answers


fprintf nested cells in matlab

Asked by ZK
on 21 Jan 2013


I have the following:

A<3x1 cell>

where cells are arranged in column:

.<1x15 cell>

.<1x15 cell>

.<1x15 cell>

I would like to fprintf content of nested cells with spaces before numbers and with 15 numbers in each row.

Whats more can it be an universal funtion, that could work even when number of cells would be different like:

A<6x1 cell>

.<1x15 cell>

.<1x15 cell>

.<1x15 cell>

.<1x15 cell>

.<1x15 cell>

.<1x13 cell>

Cells content random numbers in format Thank You

  1 Comment

Jan Simon
on 21 Jan 2013

Please post valid Matlab syntax. Neither "A<3x1 cell>" not ".<1x15 cell>" is a standard notation. Using Matlab syntax would have the enormous advanatge, that it is clear, easy to read, the common language in this forum and that it can be used directly by copy&paste to test the suggestions.


No products are associated with this question.

1 Answer

Answer by Jan Simon
on 21 Jan 2013
Edited by Jan Simon
on 21 Jan 2013
function PrintNestedCell(fid, C)
for iC = 1:numel(C)
  aC = C{iC};
  if iscell(aC)
    PrintNestedCell(fid, aC);
  elseif ischar(aC)
    fprintf('%s', aC);
  elseif isnumeric(aC)
    fprintf('%g ', aC);
    error('Class not handled: [%s]', class(aC));

This is a general idea only. I leave adding spaces and other formatting ideas up to you.

  1 Comment

on 22 Jan 2013

Thank you for answer. I am working on something simpler, but I am a long way from a good efect, fighting with regexp function, in this line.

fprintf(fid, '%s\n', [ (cell2mat(cellfun(@cell2mat, A, 'UniformOutput', false)'))]);

Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi test

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!