Variable not assigned during call to function

7 次查看(过去 30 天)
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

采纳的回答

Walter Roberson
Walter Roberson 2021-9-8
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

更多回答(1 个)

Dave B
Dave B 2021-9-7
编辑:Dave B 2021-9-7
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])
ans = 1×3
2 12 36
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
Short (very relevant) blog post on nested function sharing here: https://blogs.mathworks.com/loren/2008/01/16/nested-functions-and-variable-scope/
  1 个评论
Rudy Lagraauw
Rudy Lagraauw 2021-9-8
Hi Dave B,
Thank you for your elaborate answer, though this unfortunately does not solve my problem as I still cannot get my nested function to communicate the calculated variable to the parent function. The problem I have now, however, is of a different matter I suppose.
I hope you can help me a bit further on this. I have found an example that does more or less what I want, though except the value is not read:
function sliderchanging
% Create figure window and components
fig = uifigure('Position',[100 100 350 275]);
cg = uigauge(fig,'Position',[100 100 120 120]);
sld = uislider(fig,...
'Position',[100 75 120 3],...
'ValueChangingFcn',@(sld,event) sliderMoving(event,cg));
end
% Create ValueChangingFcn callback
function sliderMoving(event,cg)
cg.Value = event.Value;
end
The above code creates a slider and the gauge changes the needle position as the slider is moved. What I need is to read out the slider value. I thought I could manage this by adding to the above function for instance:
VALUE2 = sliderMoving(event,cg);
And change the below function to:
function VALUE1 = sliderMoving(event,cg)
cg.Value = event.Value;
VALUE1 = cg.Value;
end
I have tried this both with the below function nested and not nested. Either way I get the following error for the added line (VALUE2 = sliderMoving(event,cg);) in the above function:
Unrecognized function or variable 'event'.
Do you have any idea how to fix this?
Thanks.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by