From d8a0c3080b97e250840f8bf6b598847496ef8535 Mon Sep 17 00:00:00 2001 From: +Olivier Sauter <olivier.sauter@epfl.ch> Date: Mon, 2 Dec 2024 22:37:11 -0800 Subject: [PATCH] add cell2str.m from genlib for remote installation --- matlab/cell2str.m | 50 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 matlab/cell2str.m diff --git a/matlab/cell2str.m b/matlab/cell2str.m new file mode 100644 index 00000000..464684ef --- /dev/null +++ b/matlab/cell2str.m @@ -0,0 +1,50 @@ +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 -- GitLab