struct2fields_jd

PURPOSE ^

this script dismantle a structure into its fields in separate variables

SYNOPSIS ^

This is a script file.

DESCRIPTION ^

 this script dismantle a structure into its fields in separate variables

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 %
0002 % this script dismantle a structure into its fields in separate variables
0003 %
0004 sname = input('name of the structure to dismantle (leave empty to iteratively dismantle all structures in the workspace) : ','s');% input variable name
0005 %
0006 if ~isempty(sname),
0007     if ~exist(sname,'var'),%
0008         error(['The variable ',sname,' is not in the current workspace']);
0009     else
0010         eval(['s = ',sname]);
0011         if ~isstruct(s),
0012             error(['The variable ',sname,' is not in a structure']);
0013         else
0014             clear s
0015         end
0016     end
0017 end
0018 %
0019 flag = true;
0020 is2fjd = 0;
0021 %
0022 while flag,
0023     %
0024     if isempty(sname),
0025         %
0026         swhos = whos;
0027         %
0028         locsname = '';
0029         %
0030         for ivar = 1:length(swhos),
0031             %
0032             if strcmp(swhos(ivar).class,'struct'),
0033                 %
0034                 locsname = swhos(ivar).name;
0035                 %
0036                 break
0037                 %
0038             end
0039             %
0040         end
0041         %
0042         clear swhos ivar
0043         %
0044     else
0045         %
0046         locsname = sname;
0047         %
0048         flag = false;
0049         %
0050     end
0051     %
0052     if isempty(locsname)
0053         %
0054         flag = false;
0055         %
0056     else
0057         %
0058         eval(['fnames = fieldnames(',locsname,');'])
0059         %
0060         for iname = 1:length(fnames),
0061             %
0062             fname = fnames{iname};% field name
0063             %
0064             if isempty(sname),
0065                 if length(locsname) > 6 && strcmp(locsname(1:6),'s2fjd_'),
0066                     locfname = [s2fjd{str2double(locsname(7:end))},'_',fname];
0067                 else
0068                     locfname = [locsname,'_',fname];
0069                 end
0070             else
0071                 locfname = fname;
0072             end
0073             %
0074             if length(locfname) > 63,
0075                 %
0076                 is2fjd = is2fjd + 1;
0077                 s2fjd{is2fjd} = locfname;
0078                 locfname = ['s2fjd_',num2str(is2fjd)];
0079                 %
0080             end
0081             %
0082             eval([locfname,' = ',locsname,'.',fname,';'])
0083             %
0084             disp(['The variable ',locfname,' has been created .']);
0085             %
0086         end
0087         %
0088         clear(locsname)
0089         %
0090         clear locfname locsname fname iname fnames
0091         %
0092     end
0093 end
0094 %
0095 clear flag sname
0096 %
0097 swhos = whos;
0098 nvar = length(swhos);
0099 varsize = NaN(1,nvar);
0100 varlength = NaN(1,nvar);
0101 %
0102 for ivar = 1:nvar,
0103     %
0104     varsize(ivar) = swhos(ivar).bytes;
0105     %
0106     swhoname = swhos(ivar).name;
0107     %
0108     if length(swhoname) > 6 && strcmp(swhoname(1:6),'s2fjd_'),
0109         varlength(ivar) = length(s2fjd{str2double(swhoname(7:end))});
0110     else
0111         varlength(ivar) = length(swhoname);
0112     end
0113     %
0114 end
0115 %
0116 [varsize,isort] = sort(varsize);%,2,'descend'
0117 displength = max(varlength);
0118 %
0119 disp(' ')
0120 disp('List of variables : ')
0121 disp('--------------------')
0122 disp(' ')
0123 %
0124 for ivar = 1:nvar,
0125     %
0126     swho = swhos(isort(ivar));
0127     %
0128     if length(swho.name) > 6 && strcmp(swho.name(1:6),'s2fjd_'),
0129         swhoname = s2fjd{str2double(swho.name(7:end))};
0130     else
0131         swhoname = swho.name;
0132     end
0133     %
0134     disp([' - ',swhoname,repmat(' ',[1,displength - varlength(isort(ivar))]),': ',num2str(swho.bytes)])
0135     %
0136 end
0137 
0138 
0139 
0140 
0141 
0142 
0143

Community support and wiki are available on Redmine. Last update: 18-Apr-2019.