Skip to content
Snippets Groups Projects
Commit d8a0c308 authored by Olivier Sauter's avatar Olivier Sauter
Browse files

add cell2str.m from genlib for remote installation

parent 92e76b9d
No related branches found
No related tags found
1 merge request!180Fix gdat remote
function str = cell2str(C,n)
% function str = cell2str(C,n)
% Convert cell structure to string such that C = eval(str).
%
% [+GenLib General Purpose Library+] Swiss Plasma Center EPFL Lausanne 2022. All rights reserved.
assert(iscell(C),'C must be a cell');
assert(ndims(C)<=2,'Cell arrays with more than 2 dimensions are not supported');
[nrow,ncol] = size(C);
str = '{';
for ii=1:nrow
for jj=1:ncol
celldata = C{ii,jj};
if isstruct(celldata)
structstrcell = struct2str(celldata,n); % returns cell array of strings
datastr = '';
for kk = 1:numel(structstrcell)
datastr = [datastr,structstrcell{kk},'\n']; %#ok<AGROW>
end
datastr = sprintf(datastr);
% contatenate them to get a single string again
elseif iscell(celldata)
datastr = cell2str(celldata,n); % recursive call
else
datastr = field2str(celldata,n);
end
str = [str,datastr]; %#ok<AGROW>
if jj == ncol && ii~=nrow
str = [str,';']; %#ok<AGROW>
elseif jj~=ncol
str = [str,',']; %#ok<AGROW>
end
end
end
str = [str,'}'];
end
function datastr = field2str(data,n)
if isnumeric(data) || islogical(data)
datastr = mat2str(data,n,'class');
elseif ischar(data)
datastr = ['''',data,''''];
else
error('%s datatype not supported:',class(data))
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment