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

조회 수: 3 (최근 30일)
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
Kaustubha Govind
Kaustubha Govind 2013년 7월 10일
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Mike Hosea
Mike Hosea 2013년 11월 2일
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.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by