How do I update individual elements in matrix in Simulink?
63 views (last 30 days)
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.
Orion on 12 Nov 2014
If you really need assignment block, it works that way :
just adapt it to your problem.
Orion on 11 Nov 2014
an easy solution will be the use of a Matlab function block.
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.
Orion on 13 Nov 2014
Edited: Orion on 13 Nov 2014
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.