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 a vector that varies 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;
end
ContB=0;
BR=0;
end
%Hello, can you help me solve this error?
%Error in ==> OutAR_NotOutBR at 2
if AR<Max
{??? Output argument "PacksA" (and maybe others)
not assigned during call to
"C:\Users\vignon\Downloads\OutAR_NotOutBR.m>OutAR_NotOutBR".

Answers (1)

You don't have a branch for the AR>Max case. If you hit that condition, the output variables will not be assigned. Also, ContA doesn't appear to be set in the AR==Max case.
You should check that in all of the possible paths through your code, all of the output variables get assigned to something.

8 Comments

What happens for example if I don't use the ContA in AR==Max but what do I need it in AR>Max ?
ContA is an output of your function, so you should set it to something even if your calling function doesn't need it for a particular branch. E.g.,
ContA = [];
If you really want it to be an optional output, you could put it at the end of the list. E.g.,
function[PacksA,PacksB,ContB,AR,BR,fA,fB,ContA]=OutAR_NotOutBR(i,Max,AR,BR,fB,fA,ContB)
If I set ContA = [ ], does it not modify the value or size of the ContA vector?
Yes, of course. That is the point. You are not setting it to anything in one of your branches, so set it to something in that branch. If [] is not appropriate, then set it to something else that is appropriate.
Let's suppose that PackA is a vector of size n. and i = 1: 1: n.
Let's also suppose that every time I call the function I want to define PackA (i) as output arguments. How do I do it?
Arguments in matlab do not have parentheses. Do you have another solution?
Not sure exactly what you need to do. But here is the general syntax for having a function alter an element of a vector at the caller:
% Caller code
v = 1:n; % some arbitrary vector
for k=1:numel(v)
v(k) = some_function(some_arguments);
end
And you would have separate code for the function
% Function code
function v = some_function(some_arguments)
v = ______; % whatever expression goes into calculating the v element
end
Is that what you are trying to do?
Not entirely true, because I only need to access some elements of the PackA vector and not everything within my function
Por exemple: if i=5
function []=()
PackA(5)=7 it's an example.
As you can see, I don't need to go through all the PacksA elements.

Sign in to comment.

Categories

Asked:

on 17 Nov 2020

Commented:

on 17 Nov 2020

Community Treasure Hunt

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

Start Hunting!