How do I update individual elements in matrix in Simulink?

121 views (last 30 days)
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
Charles
Charles on 11 Nov 2014
Edited: Charles on 11 Nov 2014
I would like to note that, for my input matrix ("B" matrix), which is a vector, the "Assignment" block works exactly as I would expect.
x = 1;
y = 2;
Y0 = [0; 99; 0; 99];
U = [x , y];
Indx1 = [2 , 4];
Output:
[0 , 1 , 0 , 2]
This is the behavior I had expected from the Assignment block. If I make Y0 a matrix, though, everything breaks.
x = 1;
y = 2;
Y0 = [0 , 99; 0 , 99];
U = [x , y];
Indx1 = [3 , 4];
Output:
"Invalid input dimensions specified for input port <Y0> of 'untitled/Assignment'. None of the input ports accept a matrix when the number of output dimension is 1"
"Error in port widths or dimensions. Output port 1 of 'untitled/Empty A Matrix' is a [2x2] matrix."
Note that both errors are from changing Y0 from a 4x1 vector to a 2x2 matrix. I changed the indices to refer to the '99' elements (which is a value that, if I see, I will know that the element was not updated correctly).
The most irritating thing about this is that a vector is still a two dimensional matrix, just with one of the dimensions set to one. A two dimensional matrix of m x n can be a column vector if n = 1 or a row vector if m = 1. In this instance it is understood that m or n = 1 for all indices, but they still exist!

Sign in to comment.

Answers (3)

Orion
Orion on 12 Nov 2014
If you really need assignment block, it works that way :
just adapt it to your problem.
  1 Comment
Charles
Charles on 12 Nov 2014
Edited: Charles on 12 Nov 2014
This is part of the problem I've got, though - I don't have a 2D matrix like you are using for the 'U' input on the Assignment block. I have a bunch of scalar values that I wanted to insert.
Does this mean I need to MUX the scalars into a vector then use the Reshape block to make a matrix as an input to 'U'?
What if I only want to have 5 values input? Or any prime number of inputs (not enough inputs to fully populate an m x n matrix)
I'm not sure I understand how the values you've given for the Index vectors result in those specific replacements. Is Index Vector (1) row and (2) column? Wouldn't that just replace (1,1) and (3,3)?
Please don't think I'm dense or a Matlab/matrix novice; I got a master's earlier this year with a thesis on phased-array ground penetrating radar, with all the simulations written in Matlab. I'm no stranger to matrices, but I am a complete novice to Simulink.
It would seem like you would need to either specify the 1-dimensional matrix element number (i.e., a number 1 - 9 for your Y0/Y matrix) or you would need to specify the row/column ordered pair for each assignment.
I also don't understand how the "pull" order from the 'U' matrix is set, although that might become evident if I understood how the output ordering were assigned. This also echoes my question about what happens if I don't have enough values to populate a matrix; there will be some NULL value that I don't want but I don't see how the values are mapped from 'U' to 'Y'.

Sign in to comment.


Orion
Orion on 11 Nov 2014
Hi,
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.
  1 Comment
Charles
Charles on 12 Nov 2014
Well, this is great advice but not really the answer I was going for. I was looking for how to get the Assignment block to work for a matrix the same as it works for a vector.
If nobody posts how to do that in the next day or two I'll accept this as the best answer. Thanks.

Sign in to comment.


Orion
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.

Categories

Find more on Programmatic Model Editing 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!