# how to define what a function is doing

8 views (last 30 days)

Show older comments

I have a function that I am trying to convert to another language. Th problem is that I cannot run this because it gives me an error saying that

Error in shiftinter (line 11)

global ff rr

Output argument "val" (and maybe others) not assigned

during call to

moreover, I cannot understand what exactly each part of the code is doing. I do understand that line by line but I don't understand the function. Is there anyone who can help me to get this function run ?

to assume the input for example can be

sol = 1:100;

myop= 100:200;

function [sol,val] = function(sol, myop)

global ff rr

inter = round(sol(1)); % interpolation

sid = round(sol(2)); % sideways movement

% interpolation

if length(ff) > 0

step = (length(ff)+ inter)/(length(rr));

fff = interp1(ff,[1:step:length(ff)]);

if length(fff) >= length(rr)

fff = fff(1:length(rr));

elseif length(fff) < length(rr)

diff = length(rr)-length(fff);

fff = [fff ones(1,diff)*ff(length(ff))];

end

else

return

end

% sideways movement

if length(fff) > 0

if sid > 0

sidtill = ones(1,sid)*fff(1);

ffff = [sidtill fff(1:(length(fff) - sid))];

elseif sid <= 0

sid = abs(sid);

sidtill = ones(1,sid)*ff(length(ff));

ffff = [fff((sid+1):length(fff)) sidtill];

end

else

return

end

% scaling

rrs = real(rr)./max(real(rr));

fffs = real(ffff)./max(real(ffff));

% Evaluation by correlation coefficient --------------------

if (norm(rrs-mean(rrs)))*(norm(fffs-mean(fffs))) == 0

cc = -1e10;

else

cc = ((rrs-mean(rrs))*(fffs-mean(fffs))')/((norm(rrs-mean(rrs)))*(norm(fffs-mean(fffs))));

end

val = cc;

return

##### 1 Comment

John D'Errico
on 5 Jun 2016

### Answers (3)

Walter Roberson
on 2 Jun 2016

Your function definition is

function [sol,val] = function(sol, myop)

With sol appearing on both the left and the right, it is not necessary to assign to sol inside the routine: the input value will be copied to output. Provided, that is, that input was actually passed. So the fact you do not assign to sol is not the problem.

However, look more carefully at your code. You assign to val but only at the very bottom of the function. If your function returns early before reaching that line then nothing will have been assigned to val . Your code will execute a return if length(ff) == 0, or if length(fff) == 0 and it will do so without having assigned to val . You need to repair that.

Your ff is a global variables. One reason it could be empty is if you did not assign anything to it as global variables before calling the routine.

##### 0 Comments

Image Analyst
on 3 Jun 2016

Edited: Image Analyst
on 3 Jun 2016

Several problems. The most glaring is that you called your function "function":

function [sol,val] = function(sol, myop)

function is a reserved keyword and you should not use it as the name of one of your custom functions. Call it CalculateSol or something descriptive, but definitely not function.

The second is that for some reason you return before you ever assign val. The solution to that can definitely be determined after you follow this link. One thing you should always to is to assign all your output variables immediately after entering your function, even if it's to just assign null or something:

sol = pi; % Some default.

val = []; % Null or -1 or whatever you want.

So if you return at least it has something. Hopefully you will then inspect the return values for validity and if they're null or something, take appropriate action.

The next problem is you have a script (the first two lines of code) followed by a function in (presumably) the same m-file. You can't do that. To have more than one function in a file, you must declare the script as a function, like if your m-file is test.m, you must have all this in test.m this:

function test()

sol = 1:100;

myop= 100:200;

% Call it

[sol,val] = CalculateSol(sol, myop)

function [sol,val] = CalculateSol(sol, myop)

% Code for CalculateSol.....

So now you have two functions, which is allowed, instead of a script and a function, which is not allowed.

##### 3 Comments

Image Analyst
on 5 Jun 2016

Stalin Samuel
on 2 Jun 2016

- Your function has two return variables(sol,val).
- But you have assigned value for only one variable(val = cc)
- The error message states that "value of one or more output variables are not assigned"
- If you think variable 'val' alone fulfill your requirements then you can remove the variable 'sol'
- otherwise you must assign a value to 'sol' within the function

##### 2 Comments

Stalin Samuel
on 2 Jun 2016

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!