How to concatenate (insert, add) a scalar value into a vector using the Simulink Matlab Function block?

16 次查看(过去 30 天)
Problem using Simulink's Matlab Function Block:
I am trying to add a scalar element to a vector (B1 of m rows by 1 column) to get the vector B that will be the output of a Matlab function block. The output vector (B) is desired to have m+1 rows by one column. By concatenating the element (with value = x), the resulting output (B) has the desired dimension (m+1 by 1), but unfortunately all rows take the same value (x) of the added element.
Example 1:
function B=fcn(A,Ts)
B1 = diff(A)/Ts;
B = [1; B1];
In the example below, 1 is the value I want to index into B1 to get B. Vector A (comes from another Matlab function block) has 501 elements and I want the output of function (B) to also have 501 elements (the value of the element of the first row must be 1 and from row 2 to the last -both included- it should be the vector B1). However, all elements of B are equal to 1. "Ts" comes from a constant block with scalar value (0.02).
Example 2:
function B = fcn(A,Ts)
B1 = diff(A)/Ts;
B =[B1; 1];
Example 2 gives the same result as Example 1.
Clarification:
The problem occurs only in Simulink's Matlab Function. When I apply the following code in Windows command (with the data loaded in matlab workspace),
B1 = diff(A)/Ts;
B =[B1; 1];
I get the desired result.
Thank you.
  4 个评论
Oguz Kaan Hancioglu
How do you determine B =[B1; 1]; is desired result.
diff command calculates differences between the sequential of each element. Therefore the length of the diff command results is one smaller than the input array. Since you don't know the new value of A, you cannot assign any number to the result of the diff command.
Also you can use discrete derivative block in simulink.
Best,
Manuel Infante Francés
The result of applying diff(A) is as desired (effectively a vector of shorter length). The problem comes when I want to calculate B (I want it to be the result of concatenating a new element to diff(A) ) to obtain a vector with the dimension of A. The vector obtained (B) has the same dimension as A, but all its elements has the same value (in the example it is 1) . Why does Matlab Function give that illogical result?.
In the meantime, as an alternative I'm going to try the block you suggested.
Thank you.

请先登录,再进行评论。

回答(1 个)

Fangjun Jiang
Fangjun Jiang 2023-4-5
With variable "A" in the base workspace, you can run "diff(A)" in Command Window to get the result.
If you want to use the same "A" in base workspace but want to run "diff(A)" in a MATLAB Function block inside a Simulink model, then you need to click "Edit Data" in the MATLAB Function block editor, Add a data called "A" and specify it as a "Parameter".
That way, "A" will not come from the Inport of the MATLAB Function block. It will come from the base workspace (assume there is no "A" in model workspace).
Your code is simple. You can always add a breakpoint in the MATLAB Function block code and then run the simulation. The code will pause at the breakpoint, then run it step by step, observe the value of related variables and figure out what is wrong.
  2 个评论
Manuel Infante Francés
As I told you before, diff(A) runs perfectly in the Command Window when "A" is in the base of the workspace. But "A" is not in the workspace but is in another Matlab Function block (within the Simulink model, which is where I want it to be). Why doesn't Matlab Function concatenate correctly?. Is it a limitation?.
Thank you.
Fangjun Jiang
Fangjun Jiang 2023-4-6
  1. Turn on "Signal Dimensions" display to check if the size of the output from the other MATLAB function is correct. You could also check its value too.
  2. Inside this MATLAB Function block, put a breakpoint and observe the value of A.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Programmatic Model Editing 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by