Question

I am trying to write a script that utilizes GUIDE to visualize some results and I have stumbled to some problems. I am going to describe the task first (I will try to make it as general as possible so the question is not too long).

I have a script (main1.m) that graphs a plot and then I pick an area of the plot to analyze (I will call it point1 from now on) and produce several plots through GUIDE (guide_fun1.m). The function guide_fun1 contains 2 radio buttons (created as a group) and a slider on the x axis so the user can see the plot for a smaller range if they wish (the plots initially are from 0 to 1 on the x axis but if the user wants to see from 0 to 0.6 the slider enables them to do so).

I am showing the code main1.m (it is not in detail in order to make it short but to demonstrate what I am trying to achieve) and the guide_fun1.m below. As you see main1.m is interactive and the user can continuously click on several points of the plot until they type exit to finish the program. For every point (point1) they click, a GUIDE window with several options for graphs and plots shows up.

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

The function that I have created using GUIDE is shown below

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

I have 3 issues that I would like to fix.

  1. When the slider shows up, the slider position is at zero but since my initial plot is from 0 to 1 , I would like the slider position to be at 1. Is there any way to set the slider initial position?

  2. When I move the slider position to another value, let's say at 0.4 and then I press the next radio button ('button 2') then my graph is shown with values from 0 to 1 on the x axis but the slider position remains where it was,at the 0.4 point, so it does not look right. I would like when I press 'button 2' either to have both my slider go back to value 1 and my graph to show the plot from 0 to 1 or if my slider remains in the previous position (0.4) to have my graph show the plot from 0 to 0.4 so both the slider and the graph agree. I understand that to do that I have to figure out a way to connect the slider with the radio buttons but I do not know how to do that.

  3. When I click on the first point (point1) and the GUIDE window opens up there is no graph and none of the buttons are pressed (which is what I want). Then I push the button I want and slide the slider to the x axis range that I want to examine. But when I click another point that I want to examine (point1) in the figure(1) plot then the GUIDE window remains as is with the last button clicked and the slider where it was from the previous point and only when I click on 'radio button1' or 'radio button2' it updates to the graph of the new point1. I would like when I click on a new point1 the GUIDE window to become like the first time it opens, no plot shown and no buttons pressed (basically to reset from the previous loop).

I would appreciate any help on this, my knowledge of MATLAB is limited so any guidance on this would be really helpful.


I was able to solve some of the issues with your comments, but unfortunately not all.

  1. I solved this by adding this command set(handles.slider1,'value',1) in guide_fun1_OpeningFcn and the slider goes to 1 every time it restarts, so it works

  2. I changed in uipanel1_SelectionChangeFcn

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

so that every time I click on this button the slider goes to value 1 since my axis goes from 0 to 1 but it does not work, there is no error, it just does not update the slider when i click the first button.

3 . I am not sure how to do that. I thought every time the GUI reopens it should automatically reinitialize. Can you give me an example of what an initialization subroutine looks like?

Was it helpful?

Solution

It would be VERY helpful if you showed screenshots of your GUI.

As a starting point for your 3 issues, I would try the following:

  1. Initial slider position. Set the slider value in guide_fun1_OpeningFcn, right before guidata(hObject, handles);
  2. Updating slider position with button press. Set the slider value in the radio buttons' call backs as well as in uipanel1_SelectionChangeFcn. Don't forget to put guidata(hObject, handles); at the end of each callback or method that modifies handles. Although, the set shouldn't change handles, just a property of a handle graphics object.
  3. Make an initialization subroutine and run it from guide_fun1_OpeningFcn or, if you handles the clicks with the GUI instead of ginput, the axes click/selection callback.

A more general observation is that you should not have to put your GUI in a while loop using ginput to get input data. You could handle the axes clicks from the GUI code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top