Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Concatenate a row vector to a matrix in Simulink

943 views
Skip to first unread message

Laura

unread,
May 19, 2005, 12:45:02 AM5/19/05
to
Hello guys!

I have a new problem (once again) with my project in Simulink. I have
a model in Simulink whose output is a Mx2 matrix. Initially this
matrix is empty and in each simulation step I calculate a 1x2 vector
that I have to concatenate vertically to my matrix. The equivalent
code in Matlab would be like this:

matrix = [];
for( i=1:var )
vector = ......(calculate vector value)
matrix = [ matrix; vector ];
end

My problem is that I need to save and change the matrix value from
one simulation to another, and I don't know really how I could do it.
I have tried with 'Data store memory' but with this block I need to
know previously the dimensions of my matrix, and this dimensions
can't change from one simulation to another. In my model, initially
the matrix is empty, and in each simulation I add a new row, but I
can't know previously the total number of rows.

Can anyone help me?

Thank you very much. Regards,

Laura

Amistad

unread,
May 19, 2005, 1:53:44 AM5/19/05
to
see Matrix Concatenation
http://www.mathworks.com/access/helpdesk/help/toolbox/simulink/slref/matrixconcatenation.html#1233246

--
Amistad Hou

"Laura" <alee...@SPAMgmail.com> ??? news:ef066...@webx.raydaftYaTP
???...

Laura

unread,
May 19, 2005, 4:47:09 AM5/19/05
to
Hello Amistad,

First of all, thank you for your reading and help but I think that
the solution you gave don't work properly.
In fact, in my Simulink model I have used that block to concatenate
the vector to the matrix, so 'Concatenate' block is not my problem.

My problem is the following: I know how to concatenate a row vector
to a matrix if the matrix has the same dimensions in each simulation
step, but how can I concatenate a vector to a matrix if that matrix
is initially empty and in each simulation I add a new row?

I would like to do the following in Simulink:


matrix = [];
for( i=1:var )
vector = ......(calculate vector value)
matrix = [ matrix; vector ];
end

An example of execution of that code would be like this:
(initially)
matrix = [];

(first step)
vector = [1 1 1];
matrix = [1 1 1];

(second step)
vector = [2 2 2];
matrix = [1 1 1; 2 2 2];

(third step)
vector = [3 3 3];
matrix = [1 1 1; 2 2 2; 3 3 3];

(N step)
vector = [N N N];
matrix = [1 1 1; 2 2 2; 3 3 3; ... N N N];

I think that I need to save the matrix values at the end of each
simulation step (because I need them in the next step), but I don't
know really how to do it. I have tried with 'Data store memory' but
in the initialization of this block I need to know the final
dimension of my matrix, and each time I use 'Data Write Memory' block
I have to write a matrix of that dimension exactly, so I can't change
the dimension in each simulation step.

Has anybody any other suggestion for my code?

Thank you very much.

Laura

Frank

unread,
May 21, 2005, 12:04:07 AM5/21/05
to
Hi Laura,
try with an s-function that saves its input to the workspace at each
time step (it's the same suggestion I posted a few minutes ago for a
different thread... funny things happen sometimes!)
Here is the function:

%%%%%
function [sys,x0,str,ts] = sfunRR(t,x,u,flag)
switch flag,
case 0,
[sys,x0,str,ts] = mdlInitializeSizes(t,x,u);
case 2,
sys=mdlUpdate(t,x,u);
case 3,
sys=mdlOutputs(t,x,u);
case 9,
sys=mdlTerminate(t,x,u);
otherwise
error(['Unhandled flag = ',num2str(flag)]);
end

function [sys,x0,str,ts] =mdlInitializeSizes(t,x,u)
sizes=simsizes;
sizes.NumContStates=0;
sizes.NumDiscStates=0;
sizes.NumOutputs=0;
sizes.NumInputs=1;
sizes.DirFeedthrough=0;
sizes.NumSampleTimes=1;
sys=simsizes(sizes);
x0=[];
str=[];
ts=[0.1 0]; % I update the values each 0.1 seconds of simulation

function sys=mdlUpdate(t,x,u)

assignin('base','matrix_ti') % this could be your code

sys=[];

function sys=mdlOutputs(t,x,u)
sys=[];

function sys=mdlTerminate(t,x,u)
sys=[];
%%%%
FYI, in my model the s-function block containing this function has an
input and no outputs (you could add matrix_t(i-1) as an output...),
and no parameters as well.
Hope this helps,
Frank

Amistad Hou

unread,
May 24, 2005, 9:00:58 AM5/24/05
to
hi Laura:
in simulink, all dimensions of inputs/outputs is determined before the
simulator
starts to simulate the model. in the other words, dynamic input/outputs
dimension assignment and access are not allowed during simulation. thus,
your algorithm which uses dynamic dimension is impratical. but an
alternative
method can help you to run the same algorithm in simulink. assume the
iterations
count is known, then we can
1. allocate output dimension (matrix) of desired block for all
possible
iterations access
2. access the row of matrix at the corresponding timing

such approach avoid the uncertainty of output dimension assignment and make
the algorithm to work in simulink. moreover, some functional ports like
"enable" ,"reset", and other control signals may be added to accomplish
desired
algorithms.

"Laura" <ale...@QUITAESTO.gmail.com> ??? news:ef06...@webx.raydaftYaTP
???...

Laura

unread,
May 25, 2005, 5:06:08 AM5/25/05
to
Once again, thank you very much Amistad and Frank. I have been
thinking about what Amistad explains in the previos post to this
thread and i'm afraid that this is the only solution to my problem.
In my problem I define de variables of the Simulink model in the
Matlab Workspace, so I have created a little script that calculate
the dimensions of the final matrix based on the data I have in the
workspace, and then I create a ones matrix of this size. All of this,
before starting the simulation of the model. Then, in each simulation
step I modify the row of the matrix that corresponds to the actual
simulation step.
This works perfectly.

Thank you again!

0 new messages