wolffd@0: %CELL2STR Convert 2-D cell array into evaluable string. wolffd@0: % B = CELL2STR(C) returns a B such that C = EVAL(B), under the wolffd@0: % following contraits: wolffd@0: % - C contains only numeric, logic, string or cell arrays, not wolffd@0: % structs nor objects. wolffd@0: % - NDIMS(C) = 2. wolffd@0: % wolffd@0: % B = CELL2STR(C,N) uses N digits of precision for numeric wolffd@0: % arrays. N defaults to 15. wolffd@0: % wolffd@0: % B = CELL2STR(C,'class') and B = CELL2STR(C,N,'class') also wolffd@0: % include the class string for the numeric arrays wolffd@0: % wolffd@0: % See also MAT2STR, EVAL wolffd@0: wolffd@0: % (C) Copyright 2010-2012, All rights reserved. wolffd@0: % Cris Luengo, Uppsala, 12 August 2010. wolffd@0: % 13 July 2012: extended to work with cell arrays. wolffd@0: wolffd@0: function str = cell2str(c,n,mode) wolffd@0: wolffd@0: if nargin==2 wolffd@0: if isnumeric(n) wolffd@0: mode = ''; wolffd@0: else wolffd@0: mode = n; wolffd@0: n = 15; wolffd@0: end wolffd@0: elseif nargin==1 wolffd@0: mode = ''; wolffd@0: n = 15; wolffd@0: end wolffd@0: wolffd@0: if ~isnumeric(n) || numel(n)~=1 wolffd@0: error('Input argument N should be a scalar.') wolffd@0: end wolffd@0: if ~ischar(mode) || ~any(strcmp(mode,{'','class'})) wolffd@0: error('Mode string can only be ''class''.') wolffd@0: end wolffd@0: wolffd@0: if iscell(c) wolffd@0: N = size(c); wolffd@0: if length(N)~=2 wolffd@0: error('Cell array must be 2-dimensional.') wolffd@0: end wolffd@0: str = '{'; wolffd@0: for ii=1:N(1) wolffd@0: if ii>1 wolffd@0: str = [str,';']; wolffd@0: end wolffd@0: for jj=1:N(2) wolffd@0: if jj>1 wolffd@0: str = [str,',']; wolffd@0: end wolffd@0: str = [str,cell2str(c{ii,jj})]; wolffd@0: end wolffd@0: end wolffd@0: str = [str,'}']; wolffd@0: elseif isnumeric(c) wolffd@0: if ~isempty(mode) wolffd@0: str = mat2str(c,n,'class'); wolffd@0: else wolffd@0: str = mat2str(c,n); wolffd@0: end wolffd@0: elseif ischar(c) || islogical(c) wolffd@0: str = mat2str(c); wolffd@0: else wolffd@0: error('Illegal array in input.') wolffd@0: end