How do I update individual elements in matrix in Simulink?
Show older comments
Hello,
I have a matrix for a state-space problem I'd like to model in Simulink. The problem I've got is that I would like to use a variable in my system matrix (my "A" matrix) that I would like to update while the Simulink simulation progresses.
I know that I could have the variable be listed in the workspace and then write the new values to the workspace, but I'm concerned that (1) it would take a lot of time to write/read to/from the workspace and (2) that the values I write to the workspace won't refresh or update until the simulation is complete.
I have tried using the "Assignment" block but can't seem to get that to work to replace elements in a matrix. I tried using the default matrix (all of the values that are constants in a CONST matrix of the size I need) as the Y0 input to the "Assignment" block, then the values I would like to insert are MUX'd together as the U input, then the indices as a vector in the Indx1 input. I got an error because I was inputting a 2-dimensional matrix into the Assignment block.
Then I tried using Assignment as 2-dimensional, and assumed that Indx1 was the row and Indx2 was the column, so tried again with Y0 and U the same as before - constant matrix and time-varying matrix elements. This threw an error because it says U should be a 2-dimensional matrixi as well, but if I could make U (my time-varying elements) be a 2-dimensional matrix I wouldn't need assign!
I've been Google-ing for about an hour now but all I seem to find is information about replacing elements in a matrix in MATLAB, which is a snap. I've found information about using the "Selector" block, but that only appears to be useful if I'm looking to GET elements from a matrix, not PUT elements into a matrix.
I could always resort to building the matrix with concatenates or something similar, but these (easy) early matrices I'm working with are 4x4 and 6x4, and I'm fully expecting these to get significantly larger as my work progresses. I was hoping for some block with functionality similar to the "Replace Array Subset" block used in LabVIEW but again, all my searches seem to turn up how to do this in MATLAB and not Simulink.
Any help would be GREATLY appreciated.
Thanks,
Chuck
1 Comment
Answers (3)
Orion
on 12 Nov 2014
1 vote
If you really need assignment block, it works that way :

just adapt it to your problem.
1 Comment
Orion
on 11 Nov 2014
0 votes
Hi,
put all the inputs you need, including the initial value of your matrix. then inside this block (which is some matlab code), do all the matrix manipulation you want to do, ang get the result in the one of your output.
Note that a Matlab function block is really a simulink block, you're not calling an external mfile. And it is compatible with code generation.
I attached 2 files.
a Mscript and a model.
in both, I do the same 3 cases.
- one multiple assignment
- two consecutive single assginments
- a for loop, for the cases where the number of assignment can vary.
Note : in the third case, I use a for iterator subsystem, which is a way to implement for loops in Simulink. It's kind of tricky the first time you work with it, but I guess this might be the modeling you're looking for.
Categories
Find more on Simulink Functions in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!