How can I overcome a persistent mxArray error in Simulink (MATLAB Function Block)?

11 views (last 30 days)
Hello everyone!
As the title states, I have a problem with an mxArray error that I keep getting.
"Attempt to extract field 'NumNeighbors' from 'mxArray'."
Now, of course I did try to look up on how to overcome it, and the key is to initialize the object/variable that I am using. I actually did that with one of my data variables (using struct), and problem was solved. The problem with this second variable is that I don't even know how exactly I am to initialize it. The code that I'm trying to execute is:
clmod = fitcknn(data, 'Rank~Grade_grd_+Age_');
The thing that I am trying to do is to upload this code into a microcontroller afterwards (Arduino, Raspberry Pi), and the code, on its own as a MATLAB script, works perfectly fine, so I decided to convert it slowly to Simulink. This is the point where I'm currently stuck on. What can I do to make this function work inside a MATLAB Function Block? And yes, I do have the coder.extrinsic already layed out for every external function I'm using. What is something that I may try?
Thank you very much, and my apologies for the trouble in advance.
  1 Comment
Jean-Pierre Asdiguian
Jean-Pierre Asdiguian on 10 Apr 2022
Edited: Walter Roberson on 10 Apr 2022
Guys, I've been reading more on the forums, and the only possible solution is one that is posted by this user back in 2016, but I have absolutely no idea to make it happen:
"Hence you may go for using 'system objects' in a 'MATLAB system' block instead of MATLAB function block. Use 'interpreted execution' mode for the system block."
Would someone please explain to me (basically like tablespoon feed) the entire process for me? I have never used classref in MATLAB before, as I've never needed one. I want to create a variable of this class, and initialize it, so that I can resolve the issue with the mxArray error, but I've no clue how to do that. If necessary, I can upload the code as is (though I understand that this is not a desirable thing to do).

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 10 Apr 2022
If you were able to convert over to using Interpreted execution mode, then that would be something that you would not be able to deploy to a microcontroller, as microcontrollers cannot run interpreted MATLAB.
The general hypothesis is that you should not be doing fitting on the Microcontroller: that you should instead already have fit before hand, and saved the fit coefficients, and then the fit coefficients can be loaded into the microcontroller for use in prediction or classification.
  2 Comments
Jean-Pierre Asdiguian
Jean-Pierre Asdiguian on 10 Apr 2022
I understand what you mean. What you're saying is to have the code already run with all the necessary coefficients, and then use the "Predict" inside the microcontroller (without running the fitcknn function again) to get the output that I want. Correct? If so, I am running the code in a simple .m file, and it's working perfectly. I just want that Classification model (clmod, as I named it above) to be included in the MATLAB Function Block, but it is telling me that I'm not allowed to import such a data. Something about converting it to timeseries first (even though the data is... well... data, nothing time based). I really don't mind it having run in my IDE first, as long as it allows me to include the generated data in the end. Note that I saved the data generated in a .mat file, but I still cannot import it using the "From File" block.

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!