How should I solve the "output argument not assigned" problem(GUI function)?

1 次查看(过去 30 天)
Hello, everyone. I want to turn a image into binary image, and to determine the threshold gray level according to the resulted binary image. So I design a function, in which a GUI will pop out showing the current threshold value and the binarized image. You can modify the threshold and see the new binarized image imediately. After several modifying, if you think the binarized image is OK, you click the OK button and the function returns the current threshold. All of these GUI functionality is realized by callback functions.
But when I call that function, the error message says:
??? Output argument "threshold_value" (and maybe others) not assigned during call to
the main function is
raw_image = imread('beauty.jpg');
raw_image = rgb2gray(raw_image);
max(raw_image(:));
raw_image = im2double(raw_image);
max(raw_image(:));
threshold_value = get_threshold_value(raw_image)
The function I designed is:
function threshold_value = get_threshold_value(raw_image)
% This function is used to get the wanted threshold value, by saying wanted
% I mean the threshold judged by the user, and the value can binarize the
% image appropriately.
%%initialization
current_value = 0.5;
step_value = 0.05;
% preprocessed_image = rgb2gray(raw_image);
preprocessed_image = im2double(raw_image);
binary_image = [];
......
function ok_button_callback(source, eventdata)
threshold_value = current_value;
close(handle_new_figure)
end
end
  2 个评论
The Pirate
The Pirate 2017-7-25
The threshold_value is assigned in the callback function. But is I assign threshold_value as the default value outside the callback function. I will get the default value immediately in the main function, no matter what you have modified the threshold function.
Can you help me with this?
per isakson
per isakson 2017-7-25
编辑:per isakson 2017-7-25
"??? Output argument "threshold_value" (and maybe others) not assigned " I guess, the nested function isn't called during the execution of get_threshold_value.
Put a breakpoint at the line, threshold_value=current_value;, and run the function, get_threshold_value, to find out.

请先登录,再进行评论。

采纳的回答

Stephen23
Stephen23 2017-7-25
编辑:Stephen23 2017-7-25
function threshold_value = get_threshold_value(raw_image)
threshold_value = [];
figure_handle = figure(...);
...
waitfor(figure_handle)
end
The waitfor lets the function return the threshold_value value when the figure is closed. Here is a minimum working example:
function out = myfun(x)
out = [];
h = uicontrol('callback',@foo);
function foo(~,~)
out = x;
end
waitfor(h)
end
And tested:
>> myfun(4) % clicked the button
ans =
4
>> myfun(4) % no click
ans =
[]

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!