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

Using Multiple Matlab GUIDE Sliders to Adjust Curve Parameters

我正在使用 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 与 ay 的关系图,我希望有滑块来调整参数 Cy、Dy 和 Ey。我正在使用 Matlab R2015a。

谢谢!

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

您需要验证回调函数只执行一次 - 避免函数单独调用一次的情况。
简单的方法是检查 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


演示更优雅的解决方案:

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