Arrays from workspace into simulink

20 views (last 30 days)
Hello everyone,
in my .m-file I defined a 2d-array which I want to use in Simulink. I imported it via the "From Workspace" block. This works fine so far. When I want to use the the data contained in the array I need indexing and I have to increment the counting indices at the beginning of every iteration.
How to do that?
Regards, Christoph

Accepted Answer

Walter Roberson
Walter Roberson on 21 Oct 2022
You don't. The "From Workspace" block automatically treats the data as a signal, and treats the first column as time. You can configure it to either hold the value until the next time, or to do linear interpolation for intermediate times.
Simulink has two modes: continuous and discrete, with continuous being the default.
In continuous modes, times can be nearly anything, with it generally possible for the time to wander back and forth (if you are using the default runge-kutta solver), and with irregular timesteps. Looping through indexing based on time seldom makes sense for continuous time (but sometimes it makes sense to make an array into a parameter for an interpolation table.)
Discrete time has one event after another, fixed timestep (for any subsection, not necessarily the same for all sections). It might potentially make sense to increment through parts of an array with discrete time, but remember that the system is evolving based upon time, not based upon "step number". If the user were to configure a different timestep would it still make sense to index the array based upon step number?
If you are building parameters to blocks, then you should have a look at https://www.mathworks.com/matlabcentral/answers/1653710-global-variable-in-simulink#comment_2009495 where I show how to do that.
Generally speaking, you could create a MATLAB Function Block that had a persistent variable that was the array index, and you would use the values at the index to compute some value, and then you would increment the index (but be careful of falling off the end of the array... do you want to wrap? Do you want to hold at the last one?) But as I outline above, a lot of the time that kind of indexing turns out to be the wrong thing to do in Simulink.
What turns out to make sense sometimes is to create the workspace object with a leading column that makes sense based upon a nominal timestep, and then to From Workspace it, and let the block be a signal source with the content quietly evolving in time. Then you do not need to worry about the mapping between iteration number and (possibly variable) timesteps into column numbers

More Answers (0)

Categories

Find more on General Applications in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!