Variable not assigned during call to function
Mostra commenti meno recenti
Hi all,
I have a piece of code in which I use a function to calculate a value. This value is however obtained within a function within that function and MATLAB says that the output argument is not assigned. Here is a simple version of the function (I have removed everything from the function that is of no interest):
function [Gray_scale] = GrayScaleSlider2(I)
% Create ValueChangingFcn callback
function sliderMoving(event,TextH,im)
Gray_scale = event.Value;end
end
end
MATLAB gives the following error:
Output argument "Gray_scale" (and maybe others) not assigned during call to "GrayScaleSlider2".
Error in MenisciTracker (line 78)
[Gray_scale] = GrayScaleSlider2(Test_image1_cropped);
How do I get the function to return the calculated value?
Thanks for your help!
Best regards,
Rudy
Risposta accettata
Più risposte (1)
I would think your nested function would return a value (which can be the same as the main function if you like). Nested functions will share variables with the parent workspace, but only if the variables are defined in the parent workspace. However, I think the cleaner solution may be to just keep these separate (even if they refer to the same values).
mainfunc([1 2 3])
function argout = mainfunc(argin)
argout = nestfunc(argin.^2, argin.^3);
function subargout = nestfunc(subargin1,subargin2)
subargout = subargin1 + subargin2;
end
end
Also, if your 'internal' function doesn't need access to the workspace of your main function, consider putting it at the end. This simplifies reading the code, the subfunction is just another function (with its own workspace) in the same file:
mainfunc([1 2 3])
function argout = mainfunc(argin)
argout = nestfunc(argin.^2, argin.^3);
end
function subargout = subfunc(subargin1,subargin2)
subargout = subargin1 + subargin2;
end
More on nested functions here: https://www.mathworks.com/help/matlab/matlab_prog/nested-functions.html
More on local functions here: https://www.mathworks.com/help/matlab/matlab_prog/local-functions.html
Short (very relevant) blog post on nested function sharing here: https://blogs.mathworks.com/loren/2008/01/16/nested-functions-and-variable-scope/
Categorie
Scopri di più su Programming in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!