我想在Matlab中创建一个DISABLE按钮

时间:2014-08-28 09:52:04

标签: image-processing matlab matlab-guide

我有一个图像,滑块和一个按钮。 通过移动滑块旋转图像,按下按钮可保存图像。 还有一个写入旋转角度的文本框。 我想在用户移动到滑块之前禁用该按钮。 (滑块可以回到初始状态)

这是代码

 function [angle] = rotationGUI()

    I = imread('frames/001.jpg');
    %# c
    hFig = figure('menu','none');
    hAx = axes('Parent',hFig);

    hTxt = uicontrol('Style','text', 'Position',[290 28 20 15], 'String','0');
    uicontrol('Parent',hFig, 'Style','slider', 'Value',0, 'Min',0,...
        'Max',360, 'SliderStep',[1 10]./360, ...
        'Position',[150 5 300 20], 'Callback',{@slider_callback,I,hAx,hTxt,hFig})


    uicontrol(hFig,'Style','pushbutton','String','Save and Close',...
        'Position',[10 20 120 40],'Callback',{@ok_Callback,I,hTxt,hFig,'frames/001.jpg'});



    %# show image
    imshow(I, 'Parent',hAx)
    %# Callback function
    return;
    end
        function slider_callback(hObj, eventdata,I,hAx,hTxt,hFig)
    global angle
    global Irot
    angle = round(get(hObj,'Value'));        %# get rotation angle in degrees

    Irot = imrotate(I,angle);
    imshow(Irot, 'Parent',hAx)  %# rotate image
    if (angle==0) 
        angle=360;
    end
    set(hTxt, 'String',num2str(angle))       %# update text
    end


function ok_Callback(hObj, eventdata,I,hTxt,hFig,path1)

    global Irot
    global angle
    set(hTxt, 'String','save')
    imwrite(Irot,path1);

    delete(hFig);
    end

3 个答案:

答案 0 :(得分:1)

你需要给按钮一个这样的句柄: buttonhandle = uicontrol(...)

当你声明按钮时,你需要在按钮的组件上添加一行来禁用按钮:..., 'enable', 'off'

然后你需要将它作为参数传递给滑块函数回调,然后在滑块函数中有一行set(buttonhandle, 'enable', 'on')

并且您的按钮声明必须高于滑块声明,否则滑块回调函数会接收无效参数。

答案 1 :(得分:0)

你应该给你的按钮一个每个功能都可以访问的句柄,比如这个

    h.Button=uicontrol(hFig,'Style','pushbutton',...

然后你给其他函数提供句柄,可以通过将回调设置为{}来禁用它,或者只是不显示它,而你不希望它被点击。看起来像这样:

    set(h.Button,'callback',{});

    set(h.Button,'Visible','off');

当然,您必须再次将回调或可见选项设置为正确的回调,或者当您希望它再次运行时,将其设置为“on”。我想你会找到自己在代码中插入它的地方:)

希望这会对你有所帮助。

答案 2 :(得分:0)

感谢您的帮助!!!!! 你帮了我很多

最终代码是

function [angle] = rotationGUI()

I = imread('frames/001.jpg');
%# c
hFig = figure('menu','none');
hAx = axes('Parent',hFig);
global hButton;


hTxt = uicontrol('Style','text', 'Position',[290 28 20 15], 'String','0');
uicontrol('Parent',hFig, 'Style','slider', 'Value',0, 'Min',0,...
    'Max',360, 'SliderStep',[1 10]./360, ...
    'Position',[150 5 300 20], 'Callback',{@slider_callback,I,hAx,hTxt,hFig})

hButton=uicontrol(hFig,'Style','pushbutton','String','Save and Close',...
    'Position',[10 20 120 40],'Callback',{@ok_Callback,I,hTxt,hFig,'frames/001.jpg'});
set(hButton,'Enable','off');



imshow(I, 'Parent',hAx)
    return;
end
    function slider_callback(hObj, eventdata,I,hAx,hTxt,hFig)
global angle
global Irot
global hButton
set(hButton,'Enable','on');
angle = round(get(hObj,'Value'));        %# get rotation angle in degrees

Irot = imrotate(I,angle);
imshow(Irot, 'Parent',hAx)  %# rotate image
if (angle==0) 
    angle=360;
end
set(hTxt, 'String',num2str(angle))       %# update text
end

function ok_Callback(hObj, eventdata,I,hTxt,hFig,path1)

global Irot
global angle
set(hTxt, 'String','save')
imwrite(Irot,path1);

delete(hFig);
end
相关问题