How to Determine Field Type of All Fields and Subfields in Struct Without Loop

15 views (last 30 days)
So I have a struct that is very large (and expected to get larger) which I am using to write to a YAML file. I've found MATLABYAML and it works to write the struct beautifully into the YAML file without errors, and the same for reading the YAML file into the original struct. However, it takes quite a while to do so. I examined some of the code in the file and noticed a lot of the time spent is meticulously going through each of the fields of a struct, then through the subfields, then through whatever is inside of the subfields (in cases such as arrays and cell arrays) and converting them to the appropriate java objects (due to the use of Snakeyaml-1.9.jar written in java). This ensures the data is handled appropriately. If there is a way to get away from loops, though, I imagine it would speed up the process quite a bit. Here is an example of a part of my struct. There are multiple fields with the same format as "assembly1" as well as multiple structs within "assembly1" similar to "mtf".
testconfig.assembly1.name='Some Name';
testconfig.assembly1.mtf.config=struct(...
'overlapgroups',{{{'degree0centered','degree0vo_2'},{'degree_14','degree14'}}},...
...
'focusspec',{{'absolute',{'less',[1,2],595}}});
testconfig.assembly1.mtf.config.mtfmeasurement1=struct(...
'benchsetting','MTF Name',...
'method','OpTest7',...
'optesttype','Combination',...
'positions',{{'degree0centered','degree0vo_2','degree_14','degree14'}}...
);
testconfig.assembly1.mtf.config.degree0centered=struct('name','0 Degrees, Centered',...
'specs',[25,78;40,70;64,60]);
testconfig.assembly1.mtf.config.degree0vo_2=struct('name','0 Degrees, -2 Vertical Offset',...
'specs',[25,70;40,60;64,50]);
testconfig.assembly1.mtf.config.degree_14=struct('name','-14 Degrees, -2 Horizontal Offset',...
'specs',[25,55;40,45;0,0]);
testconfig.assembly1.mtf.config.degree14=struct('name','14 Degrees, -2 Horizontal Offset',...
'specs',[25,55;40,45;0,0]);
testconfig.assembly1.mtf.general.degree0centered=struct(...
'field_angle', 0, ...
'z_position_center', 0, ...
'z_position_from', -50, ...
'z_position_to', 150, ...
'num_defocus_planes', 21, ...
'max_defocus_frequency', 64, ...
'defocus_frequency_step', 1, ...
'x_position_center', 0, ...
'maximum_search_iterations', 5, ...
'acquisition_threshold_intensity', 5, ...
'search_threshold_intensity',5, ...
'maximum_exposure', 0.2, ...
'initial_exposure', 0.8);
A sample of the code for YAMLMATLAB is such for writing:
...
javastruct = scan(data);
...
function result = scan(r)
if ischar(r)
result = scan_char(r);
elseif iscell(r)
result = scan_cell(r);
elseif isord(r)
result = scan_ord(r);
elseif isstruct(r)
result = scan_struct(r);
elseif isnumeric(r)
result = scan_numeric(r);
elseif islogical(r)
result = scan_logical(r);
elseif isa(r,'DateTime')
result = scan_datetime(r);
else
error(['Cannot handle type: ' class(r)]);
end
end
function result = scan_struct(r)
if ~isempty(r)
result = java.util.LinkedHashMap();
for i = fields(r)'
key = i{1};
val = r.(key);
result.put(key,scan(val));
end
else
result=r;
end
end
I am using MATLAB 2017b
Thank you in advance for any help.

Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Products


Release

R2017b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!