Changing Values in Function
显示 更早的评论
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
回答(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;
Star Strider
2016-7-19
0 个投票
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 个评论
Hussam Ibrahim
2016-7-19
Star Strider
2016-7-19
You already seem to be doing that with your ‘e0’ and ‘e1’ assignments, although I did not run your code so I am not certain.
I always use the inputdlg function, simply because it’s easier. That returns a cell, so you simply have to use cell addressing on the result to recover the string.
Example:
lowThreshc = inputdlg( ... );
lowTreshold = str2num(lowThreshc{:});
Note the curly brackets ‘{}’.
I would put all this before the ‘updateTxtBox’ function regardless of how you input them.
Hussam Ibrahim
2016-7-19
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.
类别
在 帮助中心 和 File Exchange 中查找有关 Graphics Object Properties 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!