Declare array in Simulink

조회 수: 8 (최근 30일)
Guilherme Eichstadt
Guilherme Eichstadt 2018년 1월 31일
댓글: Birdman 2018년 1월 31일
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
Guilherme Eichstadt 2018년 1월 31일
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