Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

How do I use function for a function which has a matrix with variable index?

조회 수: 1 (최근 30일)
Fidele Adanvo
Fidele Adanvo 2020년 11월 17일
마감: MATLAB Answer Bot 2021년 8월 20일
function[PacksA,PacksB,ContA,ContB,AR,BR,fA,fB]=OutAR_NotOutBR(i,Max,AR,BR,fB,fA,ContB)
if AR<Max
%PacksB, PacksA, ContB, ContA are vectors that vary according to the operation
PacksB=3; fB=fB+1;
PacksA=2; ContA(AR+1)=i; AR=AR+1;
if BR==1
PacksB(ContB(1))=4; fB=fB+1;
ContB(1)=0; BR=BR-1;
elseif BR>=2
PacksB(ContB(1,1:2))=4; fB=fB+2;
if BR==2
ContB=0;
else
ContB(1)=ContB(3); ContB(1,2:3)=0;
end
BR=BR-2;
end
elseif AR==Max
PacksA=3; fA=fA+1;
PacksB=3; fB=fB+1;
if BR>0
PacksB(ContB)=4; fB=fB+BR;
ContB=0;
BR=0;
end
%Hi. What's up? Is there someone here who can help me with this error?
%Error in ==> OutAR_NotOutBR at 2 %if AR<Max
  댓글 수: 4
Geoff Hayes
Geoff Hayes 2020년 11월 18일
You may need to use the MATLAB debugger to step through the code to get an idea of what is going wrong. I'm also not clear on your comment:
This code is not all correct because when BR == 3
PacksB (ContB (1,3)) = ~ 4
ContB (1) ~ = 0
and BR=1
Why?
Fidele Adanvo
Fidele Adanvo 2020년 11월 18일
I will make the code simpler. Let's assume I have this code. And depending on the input value I want to show assign a=10 and b=5 different values (disregard the value of the output C) or assign c=100 a value and disregard a and b.
function [a,b,c] = trial_function(num)
if num==2
a=10;
b=5
else if num==4
c=100;
end
%I am getting an error. Output argument "b" (and maybe others) not assigned during call to
%Please, can you help me.Thank you.

답변 (1개)

Geoff Hayes
Geoff Hayes 2020년 11월 18일
Fidele - since your function signature is
function [a,b,c] = trial_function(num)
then you need to assign values to each of the three output variables which you are not currently doing
if num==2
a=10;
b=5
elseif num==4 % <---- note this should be elseif rather than else if
c=100;
end
The easiest solution is to just assign default values to these three variables as
a = 0;
b = 0;
c = 0;
if num==2
a=10;
b=5
elseif num==4 % <---- note this should be elseif rather than else if
c=100;
end
But is the above code really what you want? Do you want to assign different values to a and b and c if num is 2 or 4 or any other integer?

태그

Community Treasure Hunt

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

Start Hunting!

Translated by