function structstring = struct2string(var)
%STRUCT2STRING get the sentence which the input struct variable created
% structstring = struct2string(var) get the sentence which var created
% the input variable var must be a struct
%
% Example:
% clear s, s.country = 'China'; s.city = 'Chongqing';
% s.address = struct('countrty','China','city','Chongqing');
%
% structstring = struct2string(s)
%
% See also CELL2STRING.
% Copyright 2012-2020 Xibiao Studio
% Author: Xiaobiao Huang
% $Revision: 1.0.0.0 Date: $ 2012-09-27 20:22 $
if ~isstruct(var)
disp('Error: the intput variable is not a struct')
return;
end
f = fieldnames(var);
v = struct2cell(var);
structstring = [];
for i=1:length(f)
structstring = handlestruct(structstring,f{i,1},v{i,1},length(f),i);
if i ~= length(f)
structstring = [ structstring ','];
end
end
function [structstring] = handlestruct(structstring,sField,sValue,sLength,sPointer)
if ~isstruct(sValue)
if sPointer == 1
structstring = [structstring 'struct('];
end
if strcmp(class(sValue),'double')
structstring = [structstring '''' sField ''',' num2str(sValue) ];
elseif isempty(sValue)
structstring = [structstring '''' sField ''','''''];
else
structstring = [structstring '''' sField ''',''' sValue '''' ];
end
elseif isstruct(sValue)
if sPointer == 1
structstring = [structstring 'struct(''' sField ''','];
else
structstring = [structstring '''' sField ''','];
end
f = fieldnames(sValue);
v = struct2cell(sValue);
for i=1:length(f)
structstring = handlestruct(structstring,f{i,1},v{i,1},length(f),i);
if i ~= length(f)
structstring = [ structstring ','];
end
end
end
if sPointer == sLength
structstring = [ structstring ')' ];
end