How can I concatenate matrices in an embedded matlab function block in simulink?

5 views (last 30 days)
Nawal El Boghdady
Nawal El Boghdady on 5 Jul 2013
Answered: Mike Hosea on 2 Nov 2013
Hey all;
I wanted to concatenate matrices together in an embedded matlab function block, so i'd like to do something like this:
s.a = [s.a , b];
'a' is a 22x1 matrix, 'b' is also a 22x1 matrix, so in the end i'd like 's.a' to be a 22x2 matrix. The syntax above does not work and gives the following error:
================ Size mismatch (size [22 x 1] ~= size [22 x 2]). The size to the left is the size of the left-hand side of the assignment ================
I tried deleting the field 'a' and then re-adding it again after modifications using the 'remfield' and 'setfield' commands accordingly. However, the 'remfield' command is not supported by code generation, which is what I need in order to run my model on the xPC real-time target machine. Any ideas how I might fix this? Thanks in advance :)
  1 Comment
Kaustubha Govind
Kaustubha Govind on 10 Jul 2013
Typically, you cannot change the size of a variable dynamically inside the MATLAB Function block, since this is not code-generation compatible (the block always generate C code from the MATLAB code for execution). You need to declare variables as variable-size using coder.varsize to allow such constructs.

Sign in to comment.

Answers (1)

Mike Hosea
Mike Hosea on 2 Nov 2013
Kaustubha's comment is the answer to this question. Use coder.varsize to give s.a the variable-size properties that you need. For example, if you want to be able to concatenate up to 10 columns, then you could declare
coder.varsize('s.a',[22,10],[0,1]);
or use Inf instead of 10 if you don't know how many columns will be required. That particular coder.varsize line specifies that the number of rows, 22, is not variable.

Community Treasure Hunt

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

Start Hunting!