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

18 views (last 30 days)
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?

Accepted Answer

Walter Roberson
Walter Roberson on 21 Oct 2020
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.
  1 Comment
Richard Burger
Richard Burger on 21 Oct 2020
Thanks. My belief was that SignalIndex was scalar, and certainly the intention of the author of the original MATLAB was such. I have just delved into the function that provides SignalIndex and found that it uses "find" searching for a unique match of a scalar from a sample vector. Obviously "find" doesn't know the answer would be unique. I have made the output of that function take just the first element of the find. When I re-ran the coder, the error and numerous similar downstream lines of code with the same error are now compliant. Thanks for your speedy response and you were spot on.

Sign in to comment.

More Answers (0)

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!