Declare array in Simulink

8 次查看(过去 30 天)
I have a function on Simulink that copy an in variable to another according a number (such as a DEMUX, but selectable)
% code
function [device,Var_RX1, Var_RX2, Var_RX3, Var_RX4, Var_RX5, Var_RX6, Var_RX7, Var_RX8, Var_RX9, Var_RX10] = fcn(device, Var_RX)
Var_RX1 = zeros(50,1,'single');
Var_RX2 = zeros(50,1,'single');
Var_RX3 = zeros(50,1,'single');
Var_RX4 = zeros(50,1,'single');
Var_RX5 = zeros(50,1,'single');
Var_RX6 = zeros(50,1,'single');
Var_RX7 = zeros(50,1,'single');
Var_RX8 = zeros(50,1,'single');
Var_RX9 = zeros(50,1,'single');
Var_RX10 = zeros(50,1,'single');
if (device == 1)
Var_RX1 = Var_RX;
elseif (device == 2)
Var_RX2 = Var_RX;
elseif (device == 3)
Var_RX3 = Var_RX;
elseif (device == 4)
Var_RX4 = Var_RX;
elseif (device == 5)
Var_RX5 = Var_RX;
elseif (device == 6)
Var_RX6 = Var_RX;
elseif (device == 7)
Var_RX7 = Var_RX;
elseif (device == 8)
Var_RX8 = Var_RX;
elseif (device == 9)
Var_RX9 = Var_RX;
elseif (device == 10)
Var_RX10 = Var_RX;
end
end
The problem is that when the function is call I have to declare the variables (Var_RX) as zeros otherwise the follow problem occurs: "Output argument 'Var_RX1' is not assigned on some execution paths."
There's a way to declare those arrays out this function and just start the value in the beginning of the program?

采纳的回答

Guilherme Eichstadt
I solved my problem using simulink-if's blocks, then the value isn't replaced every read/write.
  1 个评论
Birdman
Birdman 2018-1-31
That is an inefficient way to do so, especially in bigger models.

请先登录,再进行评论。

更多回答(1 个)

Birdman
Birdman 2018-1-31
编辑:Birdman 2018-1-31
Simulink does not like predefined outputs at the beginning because it wants to know the size of the output, or the maximum size it gets, so that it can allocate necessary space for it. Therefore you always need to specify the size of the output, and its value of course. There is no escaping from it. But be careful to not output a variable having bigger size than predefined. Otherwise it will also throw an error. To sum up, you always need to declare its(output) beginning value and size.
Hope this helps.

类别

Help CenterFile Exchange 中查找有关 Simulink 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by