2017-03-01 476 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