Pushbutton to plot data from excel in GUI

Discussion in 'Programmer's Corner' started by youbetscramjet, Jul 19, 2007.

  1. youbetscramjet

    Thread Starter New Member

    Jul 19, 2007
    9
    0
    Hello,

    I am currently having a slightly troublesome issue with a program that I am writing. I would appreciate if anyone could offer me some guidance.

    The program consists of a GUI (both the .fig and .m files are made in GUIDE). The goal of the part of the program that is giving me issues is to plot MS Excel data selected by the user from a listbox to an axes in the program. In other words: user selects data set from list box --> user clicks the "plot" pushbutton --> a 2D plot of data from the select excel file seems to magically appear on axes in GUI


    In order to attempt to accomplish this task I have done the following:

    * FOR THE LIST BOX CALLBACK * I have used a simple "switch val" sequence and the xlsread command to get something like this:

    function listbox2_Callback(hObject, eventdata, handles)

    val = get(hObject,'val');
    switch val
    case 1
    handles.listbox2 = xlsread('test1.xls', 1, 'A2:B8');
    case 2
    handles.listbox2 =xlsread('test2.xls', 1, 'A2:B8'); ;
    return
    end
    guidata(hObject,handles)

    * FOR THE PUSH BUTTON I HAVE *

    function pushbutton1_Callback(src,evnt)
    data = str2double(get(handles.listbox2,'String'));
    x = data:),1);
    y = data:),2);
    plot(x,y)
    set(handles.axes2,'XMinorTick','on')
    grid on


    OK so I run the full code with this dysfuntional section and the figure appears - no errors. I click on one of the list box items...it thinks about it and no errors come up. I click "plot" and BANG! I get this...and needless to say no plot of x,y.

    ??? Error while evaluating uicontrol Callback

    ??? Error using ==> program2 >pushbutton1_Callback
    Too many input arguments.

    Error in ==> gui_mainfcn at 75
    feval(varargin{:});

    Error in ==> program2 at 48
    gui_mainfcn(gui_State, varargin{:});

    ??? Error using ==> program2('pushbutton1_Callback',gcbo,[],guidata(gcbo))
    Error using ==> program2>pushbutton1_Callback
    Too many input arguments.

    ??? Error while evaluating uicontrol Callback



    I am very new to programming and especially with GUI's. I've been really pulling my hair out to figure out why im getting "too many input arguments." It seems to me like the data is being found from excel from the listbox selection but i'm not sure...

    Any insight or suggestions would be very much appreciated. Thanks for reading.

    Best,
    :) Jackie

    p.s. This code is in MATLAB v.7.3 R2006b
     
  2. Dave

    Retired Moderator

    Nov 17, 2003
    6,960
    145
    It would help if you could ascertain which line is throwing the exception - use the Matlab debugger. If you don't know how to use the debugger post back and I'll tell you.

    Dave
     
  3. youbetscramjet

    Thread Starter New Member

    Jul 19, 2007
    9
    0
    Dave,

    When you say use the MATLAB debugger you mean run it step-by-step, right? I tried entering "dbstep" in the command window and it says I need to be in debug mode. I don't know how to put it in debug mode. I also tried setting up 2 breakpoints each at when x and y are defined(see pushbutton code in previous post). This was with the hopes that I could see if the code actually made it to the point where the data has been called upon at the point at which the pushbutton is initiated. I ran the code with the breakpoints and selected the pushbutton in the GUI and nothing happened other than the errors. No variables showed up in my workspace. So my debugging efforts have not been very successful and pretty inconclusive.

    However, I do know that the errors that I am getting say that they occur at lines 48 and 75; both of which I did not write -- they are part of the GUIDE program template. I have run this program many times prior to adding the listbox using other controls and I had no issue with either of these lines. Therefore, I think that issue is with the code that I posted previously that is causing an exception in 48 and 75. If it is any help tho see below for the code where MATLAB says the actual errors are occuring:

    *LINE 48 ERROR OCCURS BELOW*

    % Begin initialization code - DO NOT EDIT
    gui_Singleton = 1;
    gui_State = struct('gui_Name', mfilename, ...
    'gui_Singleton', gui_Singleton, ...
    'gui_OpeningFcn', @fatigue4_OpeningFcn, ...
    'gui_OutputFcn', @fatigue4_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{:}); (LN 48)
    else
    gui_mainfcn(gui_State, varargin{:});
    end
    % End initialization code - DO NOT EDIT

    *LINE 75 ERROR OCCURS BELOW*

    % --- Outputs from this function are returned to the command line. (LN 75)
    function varargout = fatigue4_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;



    Note that the error in line 75 is on a comment...which I find weird.


    I hope that some of this info makes the issue more clear. If you would like me to run the debugger on it please explain to me how.

    Thank you so much.
    :) Jackie
     
  4. Dave

    Retired Moderator

    Nov 17, 2003
    6,960
    145
    Hi Jackie,

    If you are getting errors generated on comments lines then there is a good chance you are experiencing some corruption between the FIG and M-files. I would recommend you firstly copy your user defined code (no code automatically generated by GUIDE) from the existing M-file and create a new GUI through GUIDE, ensuring that it creates a new corresponding FIG and M-file. copy your user defined code across and make sure you do not alter any of the initialisation code.

    Do you still get the same errors? This most probably won't correct all the errors but will allow us to pin-point where the errors exist.

    Dave
     
  5. youbetscramjet

    Thread Starter New Member

    Jul 19, 2007
    9
    0
    Dave,

    I followed your suggestion. I remade the GUI using GUIDE, had GUIDE create a new template, and re-entered my code to program the new controls. I ran the program and I got a similar but new set of errors the one highlighted in blue is the difference:


    ??? Invalid handle object.

    Error in ==> program3>pushbutton1_Callback at 504
    data = (get(handles.listbox1,'val'));


    Error in ==> gui_mainfcn at 75
    feval(varargin{:});

    Error in ==> program3 at 42
    gui_mainfcn(gui_State, varargin{:});


    The error occuring at ln 75 is just empy space after the following:

    function varargout = fatigue_5_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;

    LN 75 HERE



    I also tried one other test. Instead of having the data import from excel I just entered in some random data points that were of the same size array to see if it would work with that. I get the same set of errors regardless of if I get the data from excel or just entering it which kind of leads me to think that the listbox is fine.

    I think there is a possibility that I am using the syntax wrong when programming the pushbutton. It's almost like the pushbutton is never getting the data because it's in the wrong format. I could be wrong tho, like I said I'm new to this. Do you see anything wrong with my pushbutton syntax??

    Good suggestion tho -- funny how I get different errors by using the same code.

    Looking forward to hearing your take on this,
    :) Jackie
     
  6. Dave

    Retired Moderator

    Nov 17, 2003
    6,960
    145
    Interesting. I will have a look at this evening and post back some comments and suggestions, in the meantime can you write up the entire code of the GUI M-file in a post (please don't upload it as a seperate file). If you have several functions, can you post them up in seperate posts so that it is easier to pull them into Matlab. Thanks.

    Dave
     
  7. youbetscramjet

    Thread Starter New Member

    Jul 19, 2007
    9
    0
    P.S. --

    I tried something else that might help. I got the debugger to work with the "new" M-file. I found out that the code is definatly making it past the listbox selection and is getting caught up when x and y are defined. In other words I dont think it is able to use the "data" matrix to make x and y... if this is the case though, I really don't understand why.

    Sorry for such long winded responses. Being new to this, I don't want to overlook something that may be signifigant.

    - j
     
  8. youbetscramjet

    Thread Starter New Member

    Jul 19, 2007
    9
    0
    oops, I guess we were on at the same time... i didnt see your last post.

    I would absolutly love it if you could look at my entire code, however I am bound by a company policy -- posting generic bits and pieces is fine but releasing the entire code I is kind of iffy...and I really dont want to loose my job :p

    Do you have any more suggestions without looking at the code?

    so sorry. thanks.
    :( Jackie
     
  9. youbetscramjet

    Thread Starter New Member

    Jul 19, 2007
    9
    0
    Dave,

    I made a new GUI in GUIDE that exemplifies the section of the code that I am having trouble with in the actual program I am writing (so its ok for me to post it). The user selects one of two data sets from the listbox and clicks the pushbutton to plot on the axes-- the data sets are just numbers I randomly chose.

    If you or anyone else is willing to look this code I would really appreciate the it. I am very stuck :( ... I think I know what the issue is, I just have no idea how to fix it. I know that there is an input/output mismatch when I call the listbox handle. However, I don't understand enough about how the guidata structure works to know why.

    Here is the code:

    function varargout = plottingbuttonguitest(varargin)
    % PLOTTINGBUTTONGUITEST M-file for plottingbuttonguitest.fig
    % PLOTTINGBUTTONGUITEST, by itself, creates a new PLOTTINGBUTTONGUITEST or raises the existing
    % singleton*.
    %
    % H = PLOTTINGBUTTONGUITEST returns the handle to a new PLOTTINGBUTTONGUITEST or the handle to
    % the existing singleton*.
    %
    % PLOTTINGBUTTONGUITEST('CALLBACK',hObject,eventData,handles,...) calls the local
    % function named CALLBACK in PLOTTINGBUTTONGUITEST.M with the given input arguments.
    %
    % PLOTTINGBUTTONGUITEST('Property','Value',...) creates a new PLOTTINGBUTTONGUITEST or raises the
    % existing singleton*. Starting from the left, property value pairs are
    % applied to the GUI before plottingbuttonguitest_OpeningFunction gets called. An
    % unrecognized property name or invalid value makes property application
    % stop. All inputs are passed to plottingbuttonguitest_OpeningFcn via varargin.
    %
    % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
    % instance to run (singleton)".
    %
    % See also: GUIDE, GUIDATA, GUIHANDLES

    % Edit the above text to modify the response to help plottingbuttonguitest

    % Last Modified by GUIDE v2.5 25-Jul-2007 09:34:20

    % Begin initialization code - DO NOT EDIT
    gui_Singleton = 1;
    gui_State = struct('gui_Name', mfilename, ...
    'gui_Singleton', gui_Singleton, ...
    'gui_OpeningFcn', @plottingbuttonguitest_OpeningFcn, ...
    'gui_OutputFcn', @plottingbuttonguitest_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 plottingbuttonguitest is made visible.
    function plottingbuttonguitest_OpeningFcn(hObject, eventdata, handles, varargin)

    handles.output = hObject;

    guidata(hObject, handles);


    function varargout = plottingbuttonguitest_OutputFcn(hObject, eventdata, handles)


    varargout{1} = handles.output;


    % --- Executes on selection change in listbox1.
    function listbox1_Callback(hObject, eventdata, handles)

    val = get(hObject,'val');

    switch val
    case 1
    handles.listbox1 = [ 1 2 3 4 5 6 ; 2 4 6 8 10 12 ];
    case 2
    handles.listbox1 = [ 1 2 3 4 ; 2 4 16 256 ] ;
    return
    end

    guidata(hObject,handles)


    % --- Executes during object creation, after setting all properties.
    function listbox1_CreateFcn(hObject, eventdata, handles)

    if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
    end


    % --- Executes plotting press in pushbutton1.
    function pushbutton1_Callback(hObject, eventdata, handles)

    [x,y] = get(handles.listbox1,'string');

    plot(x,y)
    set(handles.axes1,'XMinorTick','on')
    grid on


    Also, do you know of a good MATLAB GUI book? Something that may have more information than the help menu when it comes to understanding the gui structure.


    Thank you much.
    :) Jackie
     
  10. youbetscramjet

    Thread Starter New Member

    Jul 19, 2007
    9
    0
    Here are the errors that I am getting when I select the plot button:


    ??? Error using ==> get
    Too many output arguments.

    Error in ==> plottingbuttonguitest>pushbutton1_Callback at 88
    [x,y] = get(handles.listbox1,'string');

    Error in ==> gui_mainfcn at 75
    feval(varargin{:});

    Error in ==> plottingbuttonguitest at 42
    gui_mainfcn(gui_State, varargin{:});

    ??? Error using ==> plottingbuttonguitest('pushbutton1_Callback',gcbo,[],guidata(gcbo))
    Error using ==> get
    Too many output arguments.

    ??? Error while evaluating uicontrol Callback
     
  11. youbetscramjet

    Thread Starter New Member

    Jul 19, 2007
    9
    0
    Ok...after a week of trying to figure out why this was not working I realized it was a dumb mistake. I just wanted to put it out there that I figured it out before anyone started looking at my code.

    I was entering my data in rows and asking it to plot columns. And I wasn't reseting my values to 0 before the "switch val"

    ...yeah...stupid...i know.

    Thanks for your time Dave, your advice about using the debugger was helpful -- learning the debugger helped me track down the issues.

    Best,
    Jackie
     
  12. Dave

    Retired Moderator

    Nov 17, 2003
    6,960
    145
    No problems, I'm glad to see you got a result. Having looked at your code I can see your issue - its easy when you know what is causing the problem!

    Dave
     
Loading...