Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: gui problem

Subject: gui problem

From: Adam Bright

Date: 14 Apr, 2008 22:26:02

Message: 1 of 12

Hi I've been trying to make a listbox which contains the
information from an array; this will then be used as the
start point for the graoh which i'm producing.At the moment
the script i have is using the whole range which is in the
array and is producing the graph ok. However now that i'm
trying to make the listbox work, it says "Undefined function
or variable 'time'"

However, the time array does exist otherwise it would be
impossible for the graph to show.When I type the array
'time' in the workspace, it says the same thing,so i dont
understand how its possible that the array is being used
to perform the graph but the array is showing to produce the
listbox.

my code is the following:
function varargout = gui(varargin)
% GUI M-file for gui.fig
% GUI, by itself, creates a new GUI or raises the existing
% singleton*.
%
% H = GUI returns the handle to a new GUI or the handle to
% the existing singleton*.
%
% GUI('CALLBACK',hObject,eventData,handles,...) calls
the local
% function named CALLBACK in GUI.M with the given input
arguments.
%
% GUI('Property','Value',...) creates a new GUI or
raises the
% existing singleton*. Starting from the left,
property value pairs are
% applied to the GUI before gui_OpeningFunction gets
called. An
% unrecognized property name or invalid value makes
property application
% stop. All inputs are passed to gui_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 gui

% Last Modified by GUIDE v2.5 31-Mar-2008 12:06:04

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
                   'gui_Singleton', gui_Singleton, ...
                   'gui_OpeningFcn', @gui_OpeningFcn, ...
                   'gui_OutputFcn', @gui_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 gui is made visible.
function gui_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 gui (see VARARGIN)
%Assinging Variables
[time,number,pressure,raininput] =
textread('staticvirginia.txt','%s %d %f
%f','delimiter',',','headerlines',4);
% Choose default command line output for gui
handles.output = hObject;
set(hObject,'toolbar','figure');
% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command
line.
function varargout = gui_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 on button press in Graph1.
function Graph1_Callback(hObject, eventdata, handles)
% hObject handle to Graph1 (see GCBO)
% eventdata reserved - to be defined in a future version of
MATLAB
% handles structure with handles and user data (see GUIDATA)
axes(handles.axes1)


%If statement to convert pressure into volume
case1 = (pressure < 995.7);
case2 = (pressure >= 995.7 & pressure < 1149.2);
case3 = (pressure >= 1149.2);
volume = zeros(size(pressure));
volume(case1) = ((pressure(case1)-442.95)*5.365);
volume(case2) = ((pressure(case2)-749.29)*12.034);
volume(case3) = ((pressure(case3)-934.08)*22.371);

cumvol = volume;
for i = 2:length(cumvol), if cumvol(i) < cumvol(i-1)
cumvol(i) = cumvol(i-1); end, end

zerovol = (cumvol - (cumvol(1,1)));
%values = 3000 * (ones(size(zerovol)));
runoff = zerovol / 3000;

cumrain = cumsum(raininput);

%5 minute rain values
cumrain5 = cumrain(1:5:end);
runoff5 = runoff(1:5:end);
%Discretised values for cumrain and runoff

d = 2:length(cumrain5);
disccumrain = (cumrain5(d)) - (cumrain5(d-1));

e = 2:length(runoff5);
discrunoff = (runoff5(e)) - (runoff5(e-1));


%Graph Plotting Information
timeplot = datenum(time, '"yyyy-mm-dd HH:MM:SS"');
timeplot5 = timeplot(3:5:end);


[AX,H1,H2] =
plotyy(timeplot5,disccumrain,timeplot5,discrunoff,'bar');
set(gcf, 'color', 'white');
set(H1,'BarWidth',0.4,...
  'EdgeColor',[0 0.749 0.749],...
  'FaceColor',[0 0.749 0.749],...
  'LineStyle','--')
set(H2,'BarWidth',0.4,...
  'EdgeColor',[0.6824 0.4667 0],...
  'FaceColor',[0.6824 0.4667 0],...
  'LineStyle',':')
set(AX(2),'YLim',[0 1])
set(AX(1),'YLim',[0 1])
set(AX(1),'YTick',[0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1])
set(AX(2),'YTick',[0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1])
set(get(AX(1),'Ylabel'),'String','Rainfall (mm)')
set(get(AX(2),'Ylabel'),'String','Surface Runoff (mm)')
title('Surface Runoff from green roof with rainfall
comparison');
xlabel('Time');
datetick(AX(1),'x',15)
datetick(AX(2),'x',15)
%datetick(AX(1),'x','dd-mmm-yyyy HH:MM:SS')
%datetick(AX(2),'x','dd-mmm-yyyy HH:MM:SS')
%datetick(AX(1),'x',13)
%datetick(AX(2),'x',13)
guidata(hObject, handles);

% --- Executes on button press in Graph2.
function Graph2_Callback(hObject, eventdata, handles)
% hObject handle to Graph2 (see GCBO)
% eventdata reserved - to be defined in a future version of
MATLAB
% handles structure with handles and user data (see GUIDATA)


% --- Executes on button press in Graph3.
function Graph3_Callback(hObject, eventdata, handles)
% hObject handle to Graph3 (see GCBO)
% eventdata reserved - to be defined in a future version of
MATLAB
% handles structure with handles and user data (see GUIDATA)


% --- Executes on selection change in listbox1.
function listbox1_Callback(hObject, eventdata, handles)
% hObject handle to listbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of
MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns listbox1
contents as cell array
% contents{get(hObject,'Value')} returns selected
item from listbox1

start_date = get(handles.listbox1,'Value')
guidata(hObject, handles);
% --- Executes during object creation, after setting all
properties.
function listbox1_CreateFcn(hObject, eventdata, handles)
% hObject handle to listbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of
MATLAB
% handles empty - handles not created until after all
CreateFcns called

% Hint: listbox controls usually have a white background on
Windows.
% See ISPC and COMPUTER.
set(handles.listbox1,'String',time)
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
guidata(hObject, handles);

% --- Executes on selection change in listbox2.
function listbox2_Callback(hObject, eventdata, handles)
% hObject handle to listbox2 (see GCBO)
% eventdata reserved - to be defined in a future version of
MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns listbox2
contents as cell array
% contents{get(hObject,'Value')} returns selected
item from listbox2


end_date = get(handles.listbox2,'Value');
% --- Executes during object creation, after setting all
properties.
function listbox2_CreateFcn(hObject, eventdata, handles)
% hObject handle to listbox2 (see GCBO)
% eventdata reserved - to be defined in a future version of
MATLAB
% handles empty - handles not created until after all
CreateFcns called

% Hint: listbox controls usually have a white background on
Windows.
% See ISPC and COMPUTER.

set(handles.listbox2,'String',time)
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in ClearGraph.
function ClearGraph_Callback(hObject, eventdata, handles)
% hObject handle to ClearGraph (see GCBO)
% eventdata reserved - to be defined in a future version of
MATLAB
% handles structure with handles and user data (see GUIDATA)
cla(handles.axes1,'reset')
guidata(hObject, handles);

thankyou for your help

Subject: Re: gui problem

From: Phil Goddard

Date: 14 Apr, 2008 23:18:01

Message: 2 of 12


The issue seems to be related to not understanding the
significant differences between script M-files (which you
say is working for you) and function M-files (with there
being many functions in the GUIDE generated code you've
posted).

In short, although in the function "gui_OpeningFcn" you
call textread to load your data, you need to store that
data somewhere so that it can be retrieved and used in the
function "Graph1_Callback".

The easiest way for you is probably to have something like
the line
handles.time = time;
immediately after your call to textread in gui_OpeningFcn.
Then in Graph1_Callback use handles.time rather than just
time to access the data.

(The above relies on the way that the function GUIDATA
works and also how GUIDE automatically passed the "handles"
structure as an input to Graph1_Callback.
You should look at those topics within the documentation to
get a better idea of what's going on with your code.)

Phil.

Subject: Re: gui problem

From: Steven Lord

Date: 15 Apr, 2008 02:52:04

Message: 3 of 12


"Adam Bright" <abc_services_2000@yahoo.co.uk> wrote in message
news:fu0llq$iu6$1@fred.mathworks.com...
> Hi I've been trying to make a listbox which contains the
> information from an array; this will then be used as the
> start point for the graoh which i'm producing.At the moment
> the script i have is using the whole range which is in the
> array and is producing the graph ok. However now that i'm
> trying to make the listbox work, it says "Undefined function
> or variable 'time'"
>
> However, the time array does exist otherwise it would be
> impossible for the graph to show.When I type the array
> 'time' in the workspace, it says the same thing,so i dont
> understand how its possible that the array is being used
> to perform the graph but the array is showing to produce the
> listbox.

Each function has its own workspace, where variables that are created inside
the function live until the function ends, at which point they are
destroyed. In your OpeningFcn:

*snip*

> % --- Executes just before gui is made visible.
> function gui_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 gui (see VARARGIN)
> %Assinging Variables
> [time,number,pressure,raininput] =
> textread('staticvirginia.txt','%s %d %f
> %f','delimiter',',','headerlines',4);

*snip*

you create the variable 'time' here, and it exists until:

*snip*

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

the end of your OpeningFcn, at which point it Goes Away. Later on, in your
Graph1_Callback function:

*snip*

> %Graph Plotting Information
> timeplot = datenum(time, '"yyyy-mm-dd HH:MM:SS"');

you're using time before you create it in this function.

There are a number of ways to share data between functions in a GUI. Take a
look at this page from the documentation for descriptions of the different
methods:

http://www.mathworks.com/access/helpdesk/help/techdoc/creating_guis/f13-998449.html

For more general information about functions, scripts, and workspaces, read
this documentation page:

http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f7-38085.html

--
Steve Lord
slord@mathworks.com


Subject: Re: gui problem

From: Adam Bright

Date: 15 Apr, 2008 16:08:03

Message: 4 of 12

Hi,thankyou for your responses,i've now used the guidata
command to retrieve the variable but i'm now getting the
following error:

??? Error using ==> set
Conversion to double from struct is not possible.

Error in ==> gui>listbox1_Callback at 173
    set(handles.listbox1,'String',timeplot5)

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

Error in ==> gui at 43
    gui_mainfcn(gui_State, varargin{:});

??? Error using ==>
gui('listbox1_Callback',gcbo,[],guidata(gcbo))
Error using ==> set
Conversion to double from struct is not possible.

??? Error while evaluating uicontrol Callback

i've tried putting the datestr command in front, but it says:
??? Error using ==> datestr
Cannot convert input into specified date string.
DATENUM failed.
Error using ==> datevec
The input to DATEVEC was not an array of strings..

please can someone help me..

Subject: Re: gui problem

From: Adam Bright

Date: 15 Apr, 2008 16:29:06

Message: 5 of 12

Hi,thankyou for your responses,i've now used the guidata
command to retrieve the variable but i'm now getting the
following error:

??? Error using ==> set
Conversion to double from struct is not possible.

Error in ==> gui>listbox1_Callback at 173
    set(handles.listbox1,'String',timeplot5)

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

Error in ==> gui at 43
    gui_mainfcn(gui_State, varargin{:});

??? Error using ==>
gui('listbox1_Callback',gcbo,[],guidata(gcbo))
Error using ==> set
Conversion to double from struct is not possible.

??? Error while evaluating uicontrol Callback

i've tried putting the datestr command in front, but it says:
??? Error using ==> datestr
Cannot convert input into specified date string.
DATENUM failed.
Error using ==> datevec
The input to DATEVEC was not an array of strings..

please can someone help me..

Subject: Re: gui problem

From: matt dash

Date: 15 Apr, 2008 18:22:36

Message: 6 of 12

could you post an example of what your time array looks like?

(just the first few rows should be enough)

Subject: Re: gui problem

From: Adam Bright

Date: 15 Apr, 2008 22:45:04

Message: 7 of 12

    '"2006-02-14 22:00:00"'
    '"2006-02-14 22:01:00"'
    '"2006-02-14 22:02:00"'
    '"2006-02-14 22:03:00"'
    '"2006-02-14 22:04:00"'

thank you for your help.

Subject: Re: gui problem

From: matt dash

Date: 16 Apr, 2008 16:14:02

Message: 8 of 12

Sorry for the delayed response...

The error message seems to be saying that either
handles.listbox1 or timeplot5 is a structure (which means it
has another level of variables below it, like
handles.listbox1.anothervariable.

I'm guessing it has something to do with the way you're
using guidata to get the variable you need in the callback
(since guidata usually returns a structure). If you still
need help, could you maybe post an updated version of your
code that shows how you're using guidata? (And also shows
the full code for the function listbox1_callback that is
giving the error)

If you want a "quick fix" try putting a break point at the
line that gives the error, then in your command window type
handles.listbox1 and timeplot5 and see if either of them is
a structure. (a structure will display as a list of names
followed by colons and their value). If one of them is,
maybe you can find where the value you need is stored and
just rewrite your code to point to that field of the structure.

Subject: Re: gui problem

From: Adam Bright

Date: 16 Apr, 2008 16:40:21

Message: 9 of 12

thanks for your time,

i first intially have used the gui data function just after
the plot information.

guidata(hObject, handles);
guidata(hObject, timeplot5);

immediately after this is the following:

% --- Executes on selection change in listbox1.
function listbox1_Callback(hObject, eventdata, handles)
% hObject handle to listbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of
MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns listbox1
contents as cell array
% contents{get(hObject,'Value')} returns selected
item from listbox1
    timeplot5 = guidata(hObject);
    set(handles.listbox1,'String',timeplot5)
    guidata(hObject, handles);


% --- Executes during object creation, after setting all
properties.
function listbox1_CreateFcn(hObject, eventdata, handles)
% hObject handle to listbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of
MATLAB
% handles empty - handles not created until after all
CreateFcns called

% Hint: listbox controls usually have a white background on
Windows.
% See ISPC and COMPUTER.

    start_date = get(handles.listbox1,'Value')
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
guidata(hObject, handles);

i've emailed you the full code if you think it will help.
This is much appreciated.

Subject: Re: gui problem

From: Phil Goddard

Date: 16 Apr, 2008 17:05:04

Message: 10 of 12


You don't want these two lines
> guidata(hObject, handles);
> guidata(hObject, timeplot5);

The second line is overwriting (not appending to) the first
line, and hence the handles structure is being lost.
Rather you want to add a field to the handles structure
then store the new structure, i.e.

handles.timeplot5 = timeplot5;
guidata(hObject,handles);

Then within the listbox1_Callback you want to get rid of
the following line
> timeplot5 = guidata(hObject);

and change the next line
> set(handles.listbox1,'String',timeplot5)
to
set(handles.listbox1,'String',handles.timeplot5)

In this way the info stored in the handles.timeplot5 field
(assuming they are strings) should display in to the
listbox.

Phil.

Subject: Re: gui problem

From: matt dash

Date: 16 Apr, 2008 17:09:46

Message: 11 of 12

OK, the problem is obvious now.

Since you used GUIDE to make the gui, the guidata command
should only be used to store the variable "handles". Calling
the guidata command again with something like
guidata(hObject,timeplot5) will completely overwrite your
handles structure, which is a very bad thing.

Now, when you run the callback to listbox1, the graph1
callback hasnt necessarily run yet, and so the line:
guidata(hObject,timeplot5) hasn't run, so guidata still
contains the handles variable (which is a structure) since
this is the default setting (see the line
guidata(hObject,handles) at the end of the gui_openingfcn)

so, in listbox1callback, the line timeplot5=guidata(hObject)
is just storing the handles structure under the new name
timeplot5. and your set line actually looks like:
set(handles.listbox1,'string',handles) which is what is
causing the error.

How can you fix it?

In general, you dont want to use guidata to try to store
variables like that. Instead, just trust the fact that guide
(by default) keeps the handles structure current between all
callbacks in the gui. So the easiest way to pass a variable
between two functions is to put it in the handles structure,
and let guide handle the rest. To do this, simply preface
the variable name with the word handles. instead of
timeplot5, always write it as "handles.timeplot5" (without
the quotes) and everying will be ok.

In short, until you learn enough to figure out more elegant
solutions, you can preface EVERY variable in a gui with
"handles." and things will usually work out the way you want
them to.


One more warning though: the fig file you sent me has the
graph saved as part of the fig file, which means graph
callback doesnt actually run when you load the gui. It might
 be a better idea to just run the graph callback as part of
the opening function (add the line
Graph1_Callback(hObject, [], handles
to the VERY END of the opening function)
This will ensure that the handles.timeplot5 variable gets
created before the user has a chance to click any callbacks
that rely on it.

Good luck!













Subject: Re: gui problem

From: Adam Bright

Date: 17 Apr, 2008 00:39:01

Message: 12 of 12

This is just a quick thankyou message to all thos that have
helped. I've learnt loads in the process,my graph is'nt
working but the listboxes are.I may well be posting a new
thread regarding this. But thanks once again!

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
guidata Phil Goddard 16 Apr, 2008 13:05:15
structures Phil Goddard 16 Apr, 2008 13:05:15
guide Phil Goddard 16 Apr, 2008 13:05:15
help Adam Bright 14 Apr, 2008 18:30:31
gui Adam Bright 14 Apr, 2008 18:30:31
array Adam Bright 14 Apr, 2008 18:30:31
variable Adam Bright 14 Apr, 2008 18:30:31
listbox Adam Bright 14 Apr, 2008 18:30:31
rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics