Changing Values in Function
14 次查看(过去 30 天)
显示 更早的评论
Dear Community,
I have attached below a function that is supposed to accept three inputs, and return two outputs. I am supposed to change the value of outputs. I have attached this function to my matlab script, but when I run the program. I get the following error:
Output argument "lowThreshold" (and maybe others) not assigned during call to "thresh".
Error in UsingThisThreshold (line 71)
[lowThreshold, highThreshold] = thresh(grayLevels, pixelCount, imageToThreshold);
function [lowThreshold, highThreshold] = thresh(grayLevels, pixelCount, imageToThreshold)
% create the figure
fontSize = 20;
figure
set(gcf, 'Position', get(0,'Screensize')); % Enlarge figure to full screen
subplot(1,2,1)
bar(grayLevels, pixelCount, 'BarWidth', 1, 'FaceColor', 'b');
title('Histogram of Original Integer Image', 'FontSize', fontSize);
xlim([0 max(grayLevels(:))]); % Scale x axis manually.
grid on;
subplot(1,2,2)
imshow(imageToThreshold)
title('Original Grayscale Image After Thresholding', 'FontSize', fontSize);
% create the edit boxes
%Edit boxes where the user inserts or changes data
e0 = uicontrol('Style','edit','Position',[150 80 80 20],'String','15','Callback',@updateTxtBox);
e1 = uicontrol('Style','edit','Position',[150 55 80 20],'String','500','Callback',@updateTxtBox);
[row,col] = size(imageToThreshold);
% define the callback
function updateTxtBox(object,eventdata)
lowThreshold = str2num(get(e0,'String'));
highThreshold = str2num(get(e1,'String'));
for i=1:row
for j=1:col
if imageToThreshold(i,j) < lowThreshold
imageToThreshold(i,j) = 0;
else if imageToThreshold(i,j) > highThreshold
imageToThreshold(i,j) = 0;
end
end
end
end
subplot(1,2,1)
hold on;
yAxisLimits = ylim();
line([lowThreshold, lowThreshold], yAxisLimits, 'Color', 'r', 'LineWidth', 2);
line([highThreshold, highThreshold], yAxisLimits, 'Color', 'r', 'LineWidth', 2);
caption = sprintf(' Low Threshold = %.1f', get(e0,'String'));
text(lowThreshold, 0.7*yAxisLimits(2), caption, 'Color', 'r', 'FontWeight', 'Bold', 'FontSize', 15);
caption = sprintf(' High Threshold = %.1f', get(e1,'String'));
text(highThreshold, 0.9*yAxisLimits(2), caption, 'Color', 'r', 'FontWeight', 'Bold', 'FontSize', 15);
end
end
0 个评论
回答(2 个)
Thorsten
2016-7-19
It may help to define the variables outside to the local function
% create the figure
lowThreshold = 0; % assign dummy values
highTreshold = 0;
fontSize = 20;
0 个评论
Star Strider
2016-7-19
It seems that ‘lowThreshold’ and ‘highThreshold’ are defined inside the ‘updateTxtBox’ function, but never returned from it. (While nested functions have access to variables in the function they are nested in, the reverse is not true. You have to return them as with any other function.)
I would assign them outside (before) ‘updateTxtBox’ and then, just to be safe, pass them as arguments to it.
4 个评论
Star Strider
2016-7-19
With inputdlg, you would have to put the inputdlg calls in a loop. The function has the ability to display default values, so these could initially be set to zero, and subsequently be set to the previous values entered. That is how I would do it, anyway.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Object Properties 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!