From: Scott on 8 Jul 2010 09:25 Walter Roberson <roberson(a)hushmail.com> wrote in message <hujlbg$1sq$1(a)canopus.cc.umanitoba.ca>... > Walter Roberson wrote: > > bogfrog wrote: > > > >> is there a function that can print every field in a structure, as > >> would be needed to actually access that field? > > > > Untested: > > > > sf = function subfieldnames(ThisStruct) > > sf = fieldnames(ThisStruct); > > for fnum = 1:length(sf) > > if isstruct(ThisStruct.(sf{fnum})) > > cn = subfieldnames(ThisStruct.(sf{fnum})); > > sf{fnum} = cellstr( horzcat(repmat([sf{fnum} '.'], length(cn), 1), > > char(cn))); > > end > > end > > > > > > End result will be a cell array of field names in dot format. > > > Tested version: > > function sf = subfields(ThisStruct) > sf = fieldnames(ThisStruct); > for fnum = 1:length(sf) > if isstruct(ThisStruct.(sf{fnum})) > cn = subfields(ThisStruct.(sf{fnum})); > sf(fnum) = cellstr( horzcat(repmat([sf{fnum} '.'], length(cn), 1), > char(cn))); > end > end > end I was having trouble with this code, so I made a slight fix to the sf indexing and now it works fine: function sf = subfieldnames(ThisStruct) sf = fieldnames(ThisStruct); for fnum = 1:length(sf) if isstruct(ThisStruct.(sf{fnum})) cn = subfieldnames(ThisStruct.(sf{fnum})); sf(end+1:end+length(cn)) = cellstr( horzcat(repmat([sf{fnum} '.'], length(cn), 1),char(cn))); end end end
From: Jan Simon on 8 Jul 2010 11:10 Dear Scott, > sf(end+1:end+length(cn)) = cellstr( horzcat(repmat([sf{fnum} '.'], length(cn), 1),char(cn))); Slightly simpler line: sf = cat(1, sf, strcat(sf(fnum), '.', cn)); Kind regards, Jan
|
Pages: 1 Prev: if all elements statement Next: Parallel or distributed plotting |