使用多个Matlab GUIDE滑块调整曲线参数

时间:2017-03-01 19:10:22

标签: matlab user-interface plot parameters matlab-guide

我正在使用Matlab GUIDE在单条曲线中调整多个参数。我可以使用单个滑块调整单个参数,其他所有参数都已修复,但我无法弄清楚或找到一种方法从多个滑块中获取值以调整单个曲线。到目前为止,这是我的代码:

% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
% hObject    handle to slider1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of          slider
By = get(handles.slider1,'Value');
Cy = 1.9; 
Dy = 1; 
Ey = 0.97;
ay = -15*pi/180:0.01:15*pi/180;
%alpha = -15:1:15;
Fy = (Dy*sin(Cy*atan((By*ay) - Ey*((By*ay) - atan(By*ay)))));
plot(handles.axes1,ay,Fy)

% --- Executes during object creation, after setting all properties.
function slider1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'),     get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end

我对第二个滑块和轴的代码也有相同的代码。我正在策划Fy vs ay,我想让滑块来调整参数Cy,Dy和Ey。我正在使用Matlab R2015a。

谢谢!

1 个答案:

答案 0 :(得分:0)

我认为在您的情况下最简单的解决方案是在slider2回调结束时调用slider1回调,反之亦然。

你需要验证回调函数只执行一次 - 当函数调用一个ontehr indedentlly时,避免这种情况。
这样做的简单方法是检查hObject.string(触发对象的名称)的值

更优雅的解决方案是创建另一个函数,两个回调都执行(更优雅,但需要对代码进行更多更改)。

示例:

slider1_Callback:

function slider1_Callback(hObject, eventdata, handles)
    By = ...
    %...
    plot ...

    %Verifiy slider1_Callback is not executed within slider2_Callback.
    if isequal(get(hObject, 'String'), 'slider1')
        slider2_Callback(hObject, eventdata, handles); %Execute slider2_Callback
    end

slider2_Callback:

function slider2_Callback(hObject, eventdata, handles)
    %begin of slider2 callback code
    ... 
    ...
    ...
    %end of slider2 callback code

    %Verifiy slider2_Callback is not executed within slider1_Callback.
    if isequal(get(hObject, 'String'), 'slider2')
        slider1_Callback(hObject, eventdata, handles); %Execute slider1_Callback
    end

为了验证我的答案,我使用了以下代码示例:
使用两个按钮的示例(不使用GUIDE)。

按下一个按钮时,两个按钮都会改变颜色。

%Create GUI with two buttons, without using GUIDE.
function TestNoGuide()

    %Create figure.    
    handles.fig = figure('position', [800 400 260 100]);

    handles.pushbutton1 = uicontrol('style', 'pushbutton', 'position',[10 20 100 40], 'string' , 'Button1');

    handles.pushbutton2 = uicontrol('style', 'pushbutton', 'position',[150 20 100 40], 'string' , 'Button2');

    set(handles.pushbutton1, 'callback', {@pushbutton1_Callback, handles});
    set(handles.pushbutton2, 'callback', {@pushbutton2_Callback, handles});
end

function pushbutton1_Callback(hObject, eventdata, handles)
    set(handles.pushbutton1, 'BackgroundColor', rand(1, 3));

    if isequal(get(hObject, 'String'), 'Button1')
        pushbutton2_Callback(hObject, eventdata, handles);
    end
end


function pushbutton2_Callback(hObject, eventdata, handles)
    set(handles.pushbutton2, 'BackgroundColor', rand(1, 3));

    if isequal(get(hObject, 'String'), 'Button2')
        pushbutton1_Callback(hObject, eventdata, handles);
    end
end

enter image description here

展示更优雅的解决方案:

function TestNoGuide()
    handles.fig = figure('position', [800 400 260 100]);
    handles.pushbutton1 = uicontrol('style', 'pushbutton', 'position',[10 20 100 40], 'string' , 'Button1');
    handles.pushbutton2 = uicontrol('style', 'pushbutton', 'position',[150 20 100 40], 'string' , 'Button2');
    set(handles.pushbutton1, 'callback', {@pushbutton1_Callback, handles});
    set(handles.pushbutton2, 'callback', {@pushbutton2_Callback, handles});
end

%Function is called form both callbacks
function ModifyBothButtons(handles)
    set(handles.pushbutton2, 'BackgroundColor', rand(1, 3));
    set(handles.pushbutton1, 'BackgroundColor', rand(1, 3));
end

function pushbutton1_Callback(hObject, eventdata, handles)
    ModifyBothButtons(handles);
end

function pushbutton2_Callback(hObject, eventdata, handles)
    ModifyBothButtons(handles);
end