2017-03-01 6 views
0

私はMatlab GUIDEを使用して、複数のパラメータを1つの曲線に調整しています。私は他のすべてのパラメータが固定された単一のパラメータを調整するために単一のスライダーを得ることができますが、単一の曲線を調整するために複数のスライダから値を取得する方法を見つけることはできませんでした。これまでのコードはこれまで通りです:複数の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 

私はまた、2番目のスライダと軸のコードについても同じコードを持っています。私はFyとayをプロットしており、Cy、Dy、Eyのパラメータを調整するスライダが必要です。私はMatlab R2015aを使用しています。

ありがとうございます!

答えて

0

あなたのケースで最も簡単な解決策は、slider1コールバックの末尾にslider2コールバックを呼び出すことです(逆も同様)。

コールバック関数を1回だけ実行するように修正する必要があります。関数がonedhr 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 

私の答えを確認するために、次のコードサンプルを使用しました:
2つの押しボタンを使用する例(GUIDEを使用しない)。

いずれかのボタンを押すと色が変わります。

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 
:よりエレガントなソリューションを実証

%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


関連する問題