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

26 visualizaciones (últimos 30 días)
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 comentarios
Oguz Kaan Hancioglu
Oguz Kaan Hancioglu el 5 de Abr. de 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,
Manuel Infante Francés
Manuel Infante Francés el 6 de Abr. de 2023
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.

Iniciar sesión para comentar.

Respuestas (1)

Fangjun Jiang
Fangjun Jiang el 5 de Abr. de 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 comentarios
Manuel Infante Francés
Manuel Infante Francés el 6 de Abr. de 2023
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 el 6 de Abr. de 2023
  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.

Iniciar sesión para comentar.

Categorías

Más información sobre Simulink Functions en Help Center y File Exchange.

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by