Code covered by the BSD License  

Highlights from
Swap structure values into strings

image thumbnail
from Swap structure values into strings by Zachary Danziger
Finds fields of a structure in a string, and replaces them with the corresponding field values.

struct2strrep(strctr,sin)
function sout = struct2strrep(strctr,sin)
% Swap structure values into strings
%
% sout = struct2strrep(strctr,sin)
%
% Takes the structure of strings and/or doubles (strctr) and replaces each
% occurrence of a field name in sin with the corresponding field value in
% strctr and returns the result in sout.
% 
%
% %%% ZCD November 2011
%


sout = sin;
% get caller variable name
vname = inputname(1);
% get all fields
flist = fields(strctr);

for i=1:length(flist)
    % replace each occurence of the field names with corrisponding value
    sout = strrep(sout, [vname '.' flist{i}], num2str(strctr.(flist{i})));
end

Contact us