how to define what a function is doing

조회 수: 2 (최근 30일)
Niki
Niki 2016년 6월 2일
댓글: John D'Errico 2016년 6월 5일
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
John D'Errico
John D'Errico 2016년 6월 5일
Yet another case study in why global variables are unnecessary, poor programming practice, and a breeding ground for bugs.

댓글을 달려면 로그인하십시오.

답변 (3개)

Walter Roberson
Walter Roberson 2016년 6월 2일
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.

Image Analyst
Image Analyst 2016년 6월 3일
편집: Image Analyst 2016년 6월 3일
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
Walter Roberson
Walter Roberson 2016년 6월 5일
Frankly, no, there are not enough comments here about the purpose and context of the routine, so it is a waste of our time to figure out what might be the intention and what might be wrong with it, and then to write it all up.
Image Analyst
Image Analyst 2016년 6월 5일
You can use normalized cross correlation to align signals. See normxcorr2() if you have the Image Processing Toolbox. I think it should also work for 1-D signals as well as 2-D images (like in my attached demo).

댓글을 달려면 로그인하십시오.


Stalin Samuel
Stalin Samuel 2016년 6월 2일
  • 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
Niki
Niki 2016년 6월 2일
this is not right. even if I remove the other variable, still the same error , you can run it yourself
Stalin Samuel
Stalin Samuel 2016년 6월 2일
if you run the above code probably you will get the error "Function definitions are not permitted in this context."

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Correlation and Convolution에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by