2017-03-01 480 views
0

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

% --- 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。

谢谢!

回答

0

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

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