Home Questions Loading matrix into statefow

Loading matrix into statefow

DWQA QuestionsCategory: Custom Task DevelopmentLoading matrix into statefow
Peter Holland asked 5 years ago
Hi, I am trying to set up a task in which on some trials we guide the hand along a predetermined trajectory (similar to a channel trial but with a more complex curved trajectory). In order to do this I would like to load a 'library' of trajectories from a .mat file (for example a 50x1000 matrix). I have tried using the coder.load function to make sure the matrix is loaded at compilation-time rather than run-time but I get a CPU overload (Maximum TET exceeded). This happens whether I load the data in the initialize block within the stateflow or using a simulink block and loading it there and passing it as input to the stateflow.  I've also tried the horrible hack version of printing the values of the matrix into a .m file as a function and calling that directly. Is there any way you can think of to accomplish this? Maybe somewhere in the model configuration options? The size of the matrix existing in stateflow is not the problem as we initialize it to full size and can perform processing steps on it, it seems to be getting the data from another source that's the issue. Thanks for your help!
1 Answers
Koloman Varady Staff answered 5 years ago

Hi Peter,
Probably what is happening is that, as it is being passed into Stateflow, the matrix has to be copied over and, once the C code is generated, that is a nested for loop with 50 x 1000 iterations.

My first thought is to look at whether you need the whole matrix at once. If not, you can just pass bits of the trajectory at a time.

Another thing you can try is to transpose the matrix to be 1000x50. This will improve your TET immediately (thought it might not be enough of a gain). MATLAB is a column major language https://en.wikipedia.org/wiki/Row-_and_column-major_order and it will be faster to traverse a matrix by going down the columns rather than across rows.

An alternative is you can write your own C to do this, rather than rely on the generated C and include that file among those generated (that's a more complicated thing to do but quite possible).

You could also contact Mathworks support and see if they have any ideas.