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

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 Kommentare

Fidele - what can you tell us about the input parameters? What is AR and what is Max? Is the problem the missing end for your last if statement?
if BR>0
PacksB(ContB)=4; fB=fB+BR;
end % <--- should this be here?
ContB=0;
BR=0;
Fidele Adanvo
Fidele Adanvo am 17 Nov. 2020
Bearbeitet: Fidele Adanvo am 17 Nov. 2020
AR is a value for exemple AR=1;
Max is the maximum value of AR and BR for exemple Max=3;
i,fB,fA are also values
ContB is vector.
if BR>0
PacksB(ContB)=4; fB=fB+BR;
end % <--- should this be here?
ContB=0;
BR=0;
This code is not all correct because when BR == 3
PacksB (ContB (1,3)) = ~ 4
ContB (1) ~ = 0
and BR=1
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?
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.

Antworten (1)

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?

Diese Frage ist geschlossen.

Tags

Gefragt:

am 17 Nov. 2020

Geschlossen:

am 20 Aug. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by