MATLAB GUIDE滑块位置和单选按钮

时间:2013-09-30 17:57:13

标签: matlab slider radio-button matlab-guide

我正在尝试编写一个利用GUIDE来显示某些结果的脚本,我偶然发现了一些问题。我将首先描述这个任务(我会尽量使它尽可能通用,所以问题不会太长)。

我有一个脚本(main1.m),它绘制了一个绘图,然后我选择了要分析的绘图区域(我将从现在开始称之为point1)并通过GUIDE(guide_fun1.m)生成几个绘图。函数guide_fun1包含2个单选按钮(作为一组创建)和x轴上的滑块,因此用户可以根据需要查看较小范围的图(最初的图在x轴上从0到1但是如果用户希望从0到0.6看到滑块使它们能够这样做)。

我正在展示代码main1.m(为了简化它而不是详细说明,但是为了演示我想要实现的目标)和下面的guide_fun1.m。如您所见,main1.m是交互式的,用户可以连续点击绘图的几个点,直到他们输入exit来完成程序。对于他们单击的每个点(point1),会出现一个GUIDE窗口,其中显示了多个图形和图表选项。

main1.m 
%---------------
filename='file1.mat'
load(filename)
figure(1)    
plot(data.x,data.y) %data from structure of the mat file

while 1%so that it continues asking for a region
figure(1)
'choose a point or press e to exit'
[x1,y1,key]=ginput(1) %point1(x1,y1) 

[data1y,data2y,datax]=function1(x1,y1) %function1 is an outside function that does    
%the analysis of the points x1,y1 that were picked from the user

guide_fun1(data1y,data2y,datax)

     if (key == 'e')
     display('End')
     break;
     else
     display('click point')
     end

end

我使用GUIDE创建的功能如下所示

guide_fun1.m
%-------------
function varargout = guide_fun1(varargin)

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
               'gui_Singleton',  gui_Singleton, ...
               'gui_OpeningFcn', @guide_fun1_OpeningFcn, ...
               'gui_OutputFcn',  @guide_fun1_OutputFcn, ...
               'gui_LayoutFcn',  [] , ...
               'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before guide_fun1 is made visible.
function guide_fun1_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to guide_fun1 (see VARARGIN)
handles.data1y = varargin{1};
handles.data2y = varargin{2};
handles.datax = varargin{3};

% Choose default command line output for guide_fun1
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes guide_fun1 wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = guide_fun1_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
 varargout{1} = handles.output;


% --- Executes when selected object is changed in uipanel1.
function uipanel1_SelectionChangeFcn(hObject, eventdata, handles)
% hObject    handle to the selected object in uipanel1 
% eventdata  structure with the following fields (see UIBUTTONGROUP)
%   EventName: string 'SelectionChanged' (read only)
%   OldValue: handle of the previously selected object or empty if none was selected
%   NewValue: handle of the currently selected object
% handles    structure with handles and user data (see GUIDATA)
switch get(eventdata.NewValue,'Tag') % Get Tag of selected object.

 case 'radiobutton1'
    % Code for when radiobutton1 is selected.
    semilogy(handles.datax,handles.data1y,'-r','LineWidth',4);
    axis([0 1 0. 1]);

 case 'radiobutton2'
    % Code for when radiobutton2 is selected.
    semilogy(handles.datax,handles.data2y,'-g','LineWidth',4)
    axis([0 1 0. 1]);

 end


% --- 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
xminval=0;%set min value for x axis
xmaxval=1;%set max value for x axis
xsld_step = [0.01 0.1];%slider step

axis([xminval xmaxval 0 1]);%set axis range for plot

set(hObject,'Min',xminval);
set(hObject,'Max',xmaxval);
set(hObject, 'SliderStep', xsld_step);
new_xmaxval=get(hObject,'Value')

%this is to avoid the error in case the user slides to zero
if new_xmaxval<0.01
    'min value for x axis maximum range is 0.01'
     new_xmaxval=0.01
end


axis([xminval new_xmaxval 0 1]);%set new axis range for plot

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

我有3个问题需要解决。

  1. 当滑块显示时,滑块位置为零,但由于我的初始绘图是0到1,我希望滑块位置为1.有没有办法设置滑块的初始位置

  2. 当我将滑块位置移动到另一个值时,让我们说0.4,然后按下一个单选按钮('按钮2')然后我的图形显示x轴上的值为0到1但是滑块位置保持原位,在0.4点处,因此看起来不正确。我希望当我按下'按钮2'时,我的滑块返回到值1,我的图形显示从0到1的图形,或者我的滑块是否保持在前一个位置(0.4)以使我的图形显示从0到0.4绘制图,因此滑块和图形都一致。我明白为了做到这一点,我必须找出一种方法来连接滑块和单选按钮,但我不知道该怎么做。

  3. 当我点击第一个点(point1)并打开GUIDE窗口时,没有图表,也没有按下任何按钮(这就是我想要的)。然后我按下我想要的按钮并将滑块滑动到我想要检查的x轴范围。但是当我在图(1)中点击我想要检查的另一个点(point1)时,GUIDE窗口保持不变,单击最后一个按钮,滑块位于前一个点,只有当我点击'单选按钮1'或'单选按钮2'它更新为新point1的图形。我希望当我点击一个新的点1时,GUIDE窗口变得像它第一次打开,没有显示图表而没有按下按钮(基本上是从前一个循环重置)。

  4. 我很感激任何帮助,我对MATLAB的了解有限,所以对此的任何指导都会非常有用。


    我能够解决你的评论中的一些问题,但遗憾的是并非全部。

    1. 我通过在guide_fun1_OpeningFcn中添加此命令集(handles.slider1,'value',1)解决了这个问题,每次重新启动时滑块都会变为1,所以它可以正常工作

    2. 我改变了uipanel1_SelectionChangeFcn

      案例'radiobutton1'       选择radiobutton1时的%代码。       semilogy(handles.datax,handles.data1y, ' - R', '线宽',4);       轴([0 1 0.1]);
            set(handles.slider1,'value',1);%NEW LINE

    3. 所以每次我点击这个按钮时滑块都会变为值1,因为我的轴从0变为1但是它不起作用,没有错误,它只是在我点击第一个按钮时不更新滑块

      3。我不知道该怎么做。我想每次GUI重新打开它都会自动重新初始化。你能给我一个初始化子程序看起来像什么的例子吗?

1 个答案:

答案 0 :(得分:1)

如果您显示GUI的屏幕截图,那将非常有用。

作为3个问题的起点,我会尝试以下方法:

  1. 初始滑块位置。在guide_fun1_OpeningFcn
  2. 之前的guidata(hObject, handles);中设置滑块值
  3. 按下按钮更新滑块位置。在单选按钮的回调中以及uipanel1_SelectionChangeFcn中设置滑块值。不要忘记在每个回调或修改句柄的方法结束时放置guidata(hObject, handles);。虽然set不应该更改handles,但只是句柄图形对象的属性。
  4. 创建一个初始化子例程并从guide_fun1_OpeningFcn运行它,或者,如果使用GUI而不是ginput处理点击,则点击/选择回调。
  5. 更一般的观察是,您不必使用while将GUI放在ginput循环中以获取输入数据。您可以从GUI代码处理轴单击。