Variable not assigned during call to function
7 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Rudy Lagraauw
el 7 de Sept. de 2021
Comentada: Rudy Lagraauw
el 8 de Sept. de 2021
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
0 comentarios
Respuesta aceptada
Walter Roberson
el 8 de Sept. de 2021
The only way to get the function to return the calculated value is to have it uiwait() or waitfor() the object to change status.
You are setting up a callback, which is a response to a future interaction. But setting up a callback does not cause it to wait for the event to occur; you have to request a wait if you want one.
Note too that unless you disable or delete the callback after it first fires, that the user might continue to change conditions.
It is quite common for users to pause during slider movement for long enough that an event fires, and it is also quite common for the event mechanism to fire during motion even if the user does not pause. Or, the user might go on to do something and decide to re-adjust the slider.
If you waitfor() or uiwait() for the first firing of the event, you run into the possibility that the slider is still being adjusted, and you do not take into account that the user might adjust the slider at a later time, as you are not listening for those occurances.
It is possible to set up code that can deal with repeated events, and which is outside of the sliderMoving callback, such as by setting up a listener on a POST_SET event.
However... since you are setting the guage to be the same as the slider, have you considered using a completely different mechanism? Namely using linkprop() ? https://www.mathworks.com/help/matlab/ref/linkprop.html
Más respuestas (1)
Dave B
el 7 de Sept. de 2021
Editada: Dave B
el 7 de Sept. de 2021
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/
1 comentario
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!