The most simple thing I can think of for now is:
function [myStruct] = InitStruct(myStruct, initValue)
%[
% Default arguments for demo purpose
if (nargin < 2), initValue = 42; end
if (nargin < 1), myStruct = struct('f1', '', 'f2', '', 'f3', ''); end
% Obtain all current fields in the structure
fnames = fieldnames(myStruct);
% Dynamically set all fields with initValue
for fi = 1:length(fnames),
myStruct.(fnames{fi}) = initValue;
end
%]
end
NB: It only works if the structure is scalar ... to encompass all case you can add a loop on structure's length:
function [myStruct] = InitStruct(myStruct, initValue)
%[
if (nargin < 2), initValue = 42; end
if (nargin < 1),
myStruct = struct('f1', '', 'f2', '', 'f3', '');
myStruct = repmat(myStruct, [2 6]); % Making structure not scalar
end
fnames = fieldnames(myStruct);
for fi = 1:length(fnames),
for linearIndex = 1:length(myStruct),
myStruct(linearIndex).(fnames{fi}) = initValue;
end
end
%]
end
f1,f2, etc. (i.e., letter-number pairs)? Or more complicated/arbitrary?