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)
% --- 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);
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.)
"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:
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..
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..
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.
% --- 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)
% --- 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.
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.
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.
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!
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.