Declare array in Simulink

10 views (last 30 days)
Guilherme Eichstadt
Guilherme Eichstadt on 31 Jan 2018
Commented: Birdman on 31 Jan 2018
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?

Accepted Answer

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

Sign in to comment.

More Answers (1)

Birdman
Birdman on 31 Jan 2018
Edited: Birdman on 31 Jan 2018
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.

Categories

Find more on Simulink 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!