MATLAB: Coder error – Directly accessing field or property of nonscalar struct or object not supported for code generation

matlab codernon-scalar structure

The following line of MATLAB code causes the coder error “Directly accessing field or property of nonscalar struct or object not supported for code
Generation”
healthy = ~Health(SignalIndex).HasData(SatIndex,EphemIndex);
I have seen the following Mathworks page:
The work around cited doesn’t address my particular problem (or I can’t see that it does). Can you suggest a work around please?

Best Answer

The code is not valid MATLAB unless SignalIndex is either a scalar number or else a logical array that is certain to have exactly one true in it.
Break the code into two parts. In the first part, do the structure indexing and assign the selected scalar structure to a variable. In the second part access the appropriate field of the scalar structure.