How to concatenate (insert, add) a scalar value into a vector using the Simulink Matlab Function block?
25 views (last 30 days)
Show older comments
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 Comments
Oguz Kaan Hancioglu
on 5 Apr 2023
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,
Answers (1)
Fangjun Jiang
on 5 Apr 2023
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 Comments
Fangjun Jiang
on 6 Apr 2023
- 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.
- Inside this MATLAB Function block, put a breakpoint and observe the value of A.
See Also
Categories
Find more on Simulink Functions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!