1

I'll jump to the question first, then some supporting information:

Suppose I have the following:

motor(1).Voltage = 96.2;
motor(2).Voltage = 48.0;

processingStation(1).FeedstockMotor.Voltage = 96.2;
processingStation(2).FeedstockMotor.Voltage = 48.0;

The following gives all motor voltages:

[motor.Voltage]

This does NOT give all motor voltages:

[processingStation.FeedstockMotor.Voltage]

The first output, [motor.Voltage], gives me the voltages of all the motors in the structure. How do I get the same functionality in the other case, where I'm trying to compare Voltage values for the FeedstockMotor across all processingStations?

I realize that I could rename the field FeedstockMotor_Voltage and get the same functionality, but if the discharge motor has a similar set of configurations, then I could easily setup a default motor, that has something like:

defaultMotor.Voltage = 48.0;
defaultMotor.Torque = 100;

etc., and then I could make easy assignments:

processingStation(1).FeedstockMotor = defaultMotor;

I'd like to have a very small subset of allowable motors, be able to setup highly detailed configurations for those motors, and then be able to use them.

I'd also like to be able to slice across various segments, such that I can quickly get a list of operating torques, voltages, etc. for visual trending or other HMI displays. I'd prefer not to have to loop over all of the processingStation elements to get the data I need.

Suever
  • 64,497
  • 14
  • 82
  • 101
Chuck
  • 1,977
  • 1
  • 17
  • 23
  • 1
    As the first answer below shows, check out these ideas: http://stackoverflow.com/questions/7878534/vectorization-of-multi-level-indexing-of-structs-in-matlab –  Feb 16 '17 at 03:20
  • Thanks @Jon . I looked through the answers to the question you posted and decided that having a [readable two-liner](http://stackoverflow.com/questions/42261541/how-can-i-vectorize-access-to-substructures-in-matlab?noredirect=1#comment71707455_42261575) is worth more to me than an obtuse one-liner like `squeeze(cell2mat(...))`. – Chuck Feb 16 '17 at 13:52

1 Answers1

1

You will need to first convert processingStation.FeedstockMotor to an array of structs and then you can access the Voltage field of the resulting array.

tmp = [processingStation.FeedstockMotor];
result = [tmp.Voltage];

I have a function on my path that allows me to actually access these substructures just like this

function output = rgetfield(S, field)
    % Split the fieldname on "."
    parts = regexp(field, '\.', 'once', 'split');

    output = [S.(parts{1})];

    if numel(parts) > 1
        % If there are more parts, recursively get them
        output = rgetfield(output, parts{2});
    end
end

You can then use this function like

values = rgetfield(processingStation, 'FeedstockMotor.Voltage');
Suever
  • 64,497
  • 14
  • 82
  • 101
  • I don't really like having to use a temp variable like that, but I suppose it's not the end of the world - I could use something like `FeedstockMotors = [processingStation.FeedstockMotor];` to get a group of all the motors I care and then access groups of subfields; `voltages = [FeedstockMotors.Voltage];`, or `currents = [FeedstockMotors.Current];`, etc. Again, it's still a temp variable, but that looks like the clearest/most readable way to achieve what I want, and that counts for a lot. – Chuck Feb 16 '17 at 13:46
  • @Chuck There's not much harm in a temporary variable in this case. That being said, I just added a function that I use to frequently access substructures. – Suever Feb 16 '17 at 13:55