简体   繁体   中英

dealing with nested struct access in MATLAB

What is the best way to avoid writing something like:

someDataStruct.longSubStructName.anotherLongName.theVariable =   someStruct.longSubStructName.anotherLongName.theVariable + 10;

This is too verbose and doesn't look readable. What would be the best way to deal with this in Matlab without having any objects?

Purely for readability you might split this in a few lines.

someDataStruct.longSubStructName.anotherLongName.theVariable   ...
=   someStruct.longSubStructName.anotherLongName.theVariable   ...
    + 10;

Or via a temp variable

tmp = someStruct.longSubStructName.anotherLongName.theVariable + 10;
someDataStruct.longSubStructName.anotherLongName.theVariable = tmp;

Matlab doesn't really support 'references' though, if that's what you're asking, (unless explicitly coded into a class by inheriting from the Handle class, but that's another story)

Obviously you can try hacks involving setfield / getfield and a sequence of objects, eg if you capture the 'route' to theVariable for each struct at least once:

Var1 = {someDataStruct, 'longSubStructName', 'anotherLongName', 'theVariable'};
Var2 = {someStruct, 'longSubStructName', 'anotherLongName', 'theVariable'};

then you can expand these in subsequent functional calls, eg

setfield( Var1{:}, getfield( Var2{:} ) + 10 )

but I'm not sure this is necessarily more readable tbh; it's just 'shorter'.

Equally, you might opt to create graphics objects instead of normal structs, which then do allow you to capture references to sub-objects and manipulate them directly ... but again, this probably causes more confusion in the end.

So, basically, just bite the bullet and use temporary variables.
It doesn't get any more readable than that in matlab. :)

According to matlab doc you can't access a nested struct without pass through all struct level. Maybe you can create a temp variable that starts from a middle level but is against the principle of matlab.

https://it.mathworks.com/help/matlab/matlab_prog/access-data-in-nested-structures.html

Just to add more possible syntax to your toolbox, this piece of code

someDataStruct.longSubStructName.anotherLongName.theVariable = 1;
someDataStruct.longSubStructName.anotherLongName.theVariable = ...
someDataStruct.longSubStructName.anotherLongName.theVariable + 10;

could equally be written as

n = {'longSubStructName','anotherLongName','theVariable'}; # names
someDataStruct.(n{1}).(n{2}).(n{3}) = 1;
someDataStruct.(n{1}).(n{2}).(n{3}) = someDataStruct.(n{1}).(n{2}).(n{3}) + 10;

Also; if you are using strings of uncertain origin as fieldnames (eg HDF5 attribute names which support spaces and some special characters which matlab structure field names do not), you can make sure they are valid with matlab.lang.makeValidName(fieldname) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM