I know that this is probably a simple problem but I am new to Matlab GUI's and basically want to get the old value which used to be stored in the text box to replace the value which has just been entered. E.g.
Text box contains a valid string,
User enters invalid string,
Callback func, validates input and realises new input is an error and reverts to the old previous value.
How should this be implemented or done? Atm I am just using the get and set property values.
Below is some sample code:
function sampledist_Callback(hObject, eventdata, handles)
% hObject handle to sampledist (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,'String') returns contents of sampledist as text
% str2double(get(hObject,'String')) returns contents of sampledist as a double
input = str2double(get(hObject,'String'));
if(input < 0 || input > 500)
errordlg('Sampled Dist. must be > 0 and < 500','Sample Dist - Input Error');
set(handles.sampledist,'String',['10']); %<--- I would like this value 10 to be the previous entry!
guidata(hObject,handles);
else
set(handles.sampledist,'String',['',input]);
guidata(hObject,handles);
end
Simply add a new field sampledistPrev to your handles structure.
In the openingFcn of the GUI, define the property with a line like this:
handles.sampledistPrev = 10; %# or whatever you choose as default value
%# if you want, you can set the default value to the GUI, so that you only need
%# to change it at one point, if necessary, like so:
set(handles.sampledist,'String',num2str(handles.sampledistPrev));
%# don't forget to save the handles structure at the end of the openingFcn
guidata(hObject,handles)
Then you update your callback like this:
function sampledist_Callback(hObject, eventdata, handles)
% hObject handle to sampledist (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,'String') returns contents of sampledist as text
% str2double(get(hObject,'String')) returns contents of sampledist as a double
input = str2double(get(hObject,'String'));
if(input < 0 || input > 500)
errordlg('Sampled Dist. must be > 0 and < 500','Sample Dist - Input Error');
set(handles.sampledist,'String',num2str(handles.sampledistPrev)); %reset value be the previous entry!
guidata(hObject,handles); %# Note that you don't need to save the handles structure unless
%# you have changed a user-defined value like sampledistPrev
%# It may still be useful to do it so you always remember
else
set(handles.sampledist,'String',['',input]);
%# also update the reset value
handles.sampledistPrev = input;
guidata(hObject,handles);
end
Why don't you store "the previous value" as the 'UserData' of that object, as follows:
function sampledist_Callback(hObject, eventdata, handles)
input = str2double(get(hObject,'String'));
if (input < 0 || input > 500)
errordlg('Sampled Dist. must be > 0 and < 500','Sample Dist - Input Error');
val=get(hObject,'UserData');
if isempty(val)
val='';
end
set(hObject,'String',val); %<--- This is where you'd like to set the previous entry value!
guidata(hObject,handles);
else
input=num2str(input);
set(handles.sampledist,'String',input,'UserData',input);
guidata(hObject,handles);
end
end
% Y.T.
Related
I have a code that utilizes information taken over serial, compares it to a stored string, and then changes the color values of a panel in a GUI. this is done for 2 different panels. once the strcmp for both panels is 1, a button is enabled to close the GUI. i want to use a simple closereq() function to close it, but that wasn't working. to try something different, I commented out the closereq() and added in a disp('Success'). When pressing the button, Success will appear in the command window, but only after I manually pause the program from the editor panel.
this is my code:
function varargout = WorkingGUI3(varargin)
% WORKINGGUI3 MATLAB code for WorkingGUI3.fig
% WORKINGGUI3, by itself, creates a new WORKINGGUI3 or raises the existing
% singleton*.
%
% H = WORKINGGUI3 returns the handle to a new WORKINGGUI3 or the handle to
% the existing singleton*.
%
% WORKINGGUI3('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in WORKINGGUI3.M with the given input arguments.
%
% WORKINGGUI3('Property','Value',...) creates a new WORKINGGUI3 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before WorkingGUI3_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to WorkingGUI3_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 WorkingGUI3
% Last Modified by GUIDE v2.5 02-Oct-2019 14:53:57
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #WorkingGUI3_OpeningFcn, ...
'gui_OutputFcn', #WorkingGUI3_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 WorkingGUI3 is made visible.
function WorkingGUI3_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 WorkingGUI3 (see VARARGIN)
handles.uipanels = [handles.uipanel1, handles.uipanel2];
set(handles.pushbutton5, 'enable', 'off');
% Choose default command line output for WorkingGUI3
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes WorkingGUI3 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = WorkingGUI3_OutputFcn(hObject, eventdata, handles)
delete(instrfind('Port', 'COM3'));
tag = serial('COM3');
fopen(tag);
BOX = char(zeros(2,14)); % matrix to be populated with incoming serial data
TrueValueData = 'C:\RfidChipTrueValues.xlsx';
[~,~,TrueValMat] = xlsread(TrueValueData);
% Creates matrix filled with the correct values
% indexed by box, which is the first row
% all proceeding rows are the master value
for i=1:inf
for n = 1:2
if i>10
readData = fscanf(tag);
if length(readData)>12
BOX(str2double(readData(8)),1:14)= readData(11:24);
if strcmp(TrueValMat{2,n}, BOX(n,:))
set(handles.uipanels(n), 'BackgroundColor', 'g');
else
set(handles.uipanels(n), 'BackgroundColor', 'r');
end
drawnow
if strcmp(TrueValMat{2,1}, BOX(1,:))...
&& strcmp(TrueValMat{2,2}, BOX(2,:)) == 1
set(handles.pushbutton5, 'enable', 'on');
end
end
end
end
end
% 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 pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%closereq();
disp('It Works')
the final section
% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%closereq();
disp('Success')
holds the callback to the button. I know its it correctly connected to the .fig because pressing the button makes Success appear, but only after manually pausing the program.
any help would be much appreciated.
As #Rotem showed, adding a pause after the first for loop successfully allowed the program to close.
I have a script called 'main.m' that basically takes the paths where I've saved all my images and insert them in arrays. It saves the images name in a .dat file and call a function named 'selectFolder.m'.
I posted all the script and functions under, my request is at the bottom.
%% Folders
imgFolder = './1.Dataset/';
functFolder = './2.Functions/' ;
%resFolder = './3.Results/';
%% Add path
addpath(genpath(imgFolder));
addpath(genpath(functFolder));
%% Listing Folders where my images are at
myFolder1 = '../Always'; %folder path
[..] %12 folders in total
myFolder12 = '../Random'; %folder path
%% Distinguish folder 'Always' & 'Random'
% Always Folders: subset of images for all users
mfA = {myFolder1, myFolder3, myFolder5, myFolder7, myFolder9, myFolder11};
dimA = length(mfA);
% Random Folders: subset of images randomly showed
mfR = {myFolder2, myFolder4, myFolder6, myFolder8, myFolder10, myFolder12};
dimR = length(mfR);
% check if folders are present
for i = 1:dimA
if ~isdir(mfA{i})
errorMessage = sprintf('Error: The following folder does not exist:\n%s', mfA{i});
uiwait(warndlg(errorMessage));
return;
end
end
for j = 1:dimR
if ~isdir(mfR{j})
errorMessage = sprintf('Error: The following folder does not exist:\n%s', mfR{j});
uiwait(warndlg(errorMessage));
return;
end
end
%% Take images and insert'em in Arrays
% Always
MyImgs1 = dir(fullfile(mfA{1}, '*.jpg'));
[..] %for every cell
MyImgs6 = dir(fullfile(mfA{6}, '*.jpg'));
% Random
MyImgs1r = dir(fullfile(mfR{1}, '*.jpg'));
[..] %for every cell
MyImgs6r = dir(fullfile(mfR{6}, '*.jpg'));
% create arrays with images names
Array_mfA = {MyImgs1.name, MyImgs2.name, MyImgs3.name, MyImgs4.name, MyImgs5.name, MyImgs6.name};
Array_mfR = {MyImgs1r.name, MyImgs2r.name, MyImgs3r.name, MyImgs4r.name, MyImgs5r.name, MyImgs6r.name};
%% Print content of array on file
fileIDA = fopen('2.Functions/Array_Always.dat','w');
formatSpec = '%s,';
nrows = length(Array_mfA);
for row = 1 : nrows
fprintf(fileIDA, formatSpec, Array_mfA{row});
end
fclose(fileIDA);
fileIDR = fopen('2.Functions/Array_Random.dat','w');
formatSpec = '%s,';
nrows = length(Array_mfR);
for row = 1 : nrows
fprintf(fileIDR, formatSpec, Array_mfR{row});
end
fclose(fileIDR);
%disclaimer
nrc = 1;
file = fopen('2.Functions/disclaimer.dat', 'w');
fprintf(file, '%d', nrc);
fclose(file);
%% call function
selectFolder(mfA, mfR);
This function takes two array as input, these array contains all the names of my images sorted. It does some operation and then it calls another function 'selectImage.m' that displays fullscreen the selected image.
function [] = selectFolder(mfA, mfR)
clc
%% Open Arrays from file
% Always
fileID = fopen('2.Functions/Array_Always.dat', 'rt');
Array_A = textscan(fileID,'%s', 'Delimiter', ',');
fclose(fileID);
% Random
fileID2 = fopen('2.Functions/Array_Random.dat', 'rt');
Array_R = textscan(fileID2,'%s', 'Delimiter', ',');
fclose(fileID2);
%% Show Disclaimer
file = fopen('2.Functions/disclaimer.dat', 'r');
dis = fscanf(file, '%d');
fclose(file);
if (dis == 1)
set(gcf,'Toolbar','none','Menubar','none', 'NumberTitle','off');
set(gcf,'units','normalized','outerposition',[0 0 1 1]);
hAx = gca;
set(hAx,'Unit','normalized','Position',[0 0 1 1]);
imshow('1.Dataset/Disclaimer/DIS.jpg');
drawnow;
nrc = 0;
file = fopen('2.Functions/disclaimer.dat', 'w');
fprintf(file, '%d', nrc);
fclose(file);
return;
end
%% select random folder from 'Array_A' aka Always Array
dimA = length(mfA);
if ~isempty(Array_A{1})
rndn = randperm(dimA, 1);
A_check = Array_A;
while isequal(A_check,Array_A)
Array_A = selectImage(mfA{rndn}, Array_A);
if isequal(A_check,Array_A)
rndn = randperm(dimA, 1);
end
end
fileIDA = fopen('2.Functions/Array_Always.dat','w');
formatSpec = '%s,';
nrows = cellfun('length', Array_A);
for row = 1 : nrows
fprintf(fileIDA, formatSpec, Array_A{1}{row});
end
fclose(fileIDA);
return;
end
%% select random folder from 'Array_R' aka Random Array
if ~isempty(Array_R{1})
dimR = length(mfR);
rndnr = randperm(dimR, 1);
R_check = Array_R;
while isequal(R_check,Array_R)
Array_R = selectImage(mfR{rndnr}, Array_R);
if isequal(R_check, Array_R)
rndnr = randperm(dimR, 1);
end
end
fileIDR = fopen('2.Functions/Array_Random.dat','w');
formatSpec = '%s,';
nrows = cellfun('length', Array_R);
for row = 1 : nrows
fprintf(fileIDR, formatSpec, Array_R{1}{row});
end
fclose(fileIDR);
end
end
selectImage:
function [ Array ] = selectImage( myFolder, Array )
%% Check
MyImgs = dir(fullfile(myFolder, '*.jpg'));
dim = length(MyImgs);
n = 0;
for i = 1 : dim
MyImgs(i).name
if ~any(strcmp(Array{1}, MyImgs(i).name))
disp(MyImgs(i).name);disp('not present in ');disp(myFolder);
n = n + 1;
end
end
if (n == dim)
disp('empty folder')
return;
end
rN = randperm(dim, 1);
baseFileName = MyImgs(rN).name;
while ~any(strcmp(Array{1}, baseFileName))
fprintf(1, 'not present %s\n', baseFileName);
rN = randperm(dim, 1);
baseFileName = MyImgs(rN).name;
end
%% Dispay image
dim = cellfun('length', Array);
for i = 1 : dim
if strcmp(baseFileName, Array{1}(i))
Array{1}(i) = [];
break
end
end
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
imageArray = imread(fullFileName);
set(gcf,'Toolbar','none','Menubar','none', 'NumberTitle','off');
set(gcf,'units','normalized','outerposition',[0 0 1 1]);
hAx = gca;
set(hAx,'Unit','normalized','Position',[0 0 1 1]);
imshow(imageArray); % Display image.
drawnow;
end
Now I have to integrate these functions in my gui. What I want to do is call the 'main.m' script just one time with a button like 'Let's Start' and with that will show the disclaimer.
Then repeat the process calling only the 'Next' button, which calls 'selectFolder.m' and display the images with the procedure described above.
Is it possibile to do it this way? I mean, how can I pass the variable 'mfA' and 'mfR' to selectFolder? Is there a better and simpler way to do it?
The code in the gui is like:
-main:
% --- Executes on button press in Start.
function Start_Callback(hObject, eventdata, handles)
% hObject handle to Start (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);
figure
main
-selectFolder:
function Next_Callback(hObject, eventdata, handles)
% hObject handle to Next (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);
figure %show the image in another window
selectFolder(mfA, mfR)
An easy way to share variables among the callback of a GUI is to use the
guidata function.
With respect to your specific variables mfA and mfR you can use guidata
to store them, this way: in the callback in which you generate the variables you want to share with other callback you can insert the following code:
% Get the GUI data
my_guidata=guidata(gcf);
%
% section of your code in which you create the mfA and mfR vars
%
% Store the variables to be shared among the callbacks in the guidata
my_guidata.mfA=mfA;
my_guidata.mfR=mfR;
% Save the updated GUI data
guidata(gcf,my_guidata);
In the callback in which you wnat to retreive the data, you can insert the
following code:
% Get the GUI data
my_guidata=guidata(gcf);
% Retrieve the data from the GUI data structure
mfA=my_guidata.mfA;
mfR=my_guidata.mfR;
In both the examples, the struct my_guidata holds the handles of the GUI and the additional varaibles you have defined.
With respect to the the architecture of the GUI, there are lots of possibilities.
Firt a comment: looking at the two callback you've posted at the bottom of your question, it seems that your GUI has, at least, one axes, nevertheless, you create, in both of them a new figure so it is not clear the role of that axes
Considering now your questions
What I want to do is call the 'main.m' script just one time with a button like 'Let's Start' and with that will show the disclaimer. Then repeat the process calling only the 'Next' button, which calls 'selectFolder.m' and display the images with the procedure described above
call the 'main.m' script just one time with a button like 'Let's Start' and with that will show the disclaimer
You have just to copy the relevant code of your main in the Start pushbutton callback.
Notice that the code which shows the disclaimer is actually in your selectFolder function, so you have to move it in the
Start callback.
Then repeat the process calling only the 'Next' button, which calls 'selectFolder.m' and display the images with the procedure described above
to do this, you have to remove the call to selectFolder from the main and move the body of your in the Next pushbotton callback.
Also you can copy the selectImage in the GUI .m file.
Hope this helps.
Qapla'
I'm working on basic GUI in Matlab-2012a. I wanted to display the select and display the image using a push button.
Here's my code:
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global TrainingData;
global filenames;
TrainingData={};
[filenames, pathname] = uigetfile({'*.jpg';'*.png';'*.bmp'});
if ~ischar(filenames) % on cancel press you display a message of error with errordlg
errordlg('Error!','No file selected'); % displays an error message by means of errordlg function
return;
end
axes(handles.myaxesImage);
imshow(filenames);
I can browse the image, but I'm not able to display the same. I'm getting the following Error Message:
Reference to non-existent field 'axesImage'.
Error in GUI>pushbutton2_Callback (line 93)
axes(handles.axesImage);
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in GUI (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
#(hObject,eventdata)GUI('pushbutton2_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
Any Suggestions?
Thank you in advance.
According to your code
axes(handles.myaxesImage);
MATLAB said
Reference to non-existent field 'axesImage'
So there is a problem for MATLAB to recognize object handles.myaxesImage
Because it doesn't exist!
Here is an example
In the .m file of my GUI
I want to show image on axes whose tag is axes1
function openFile_Callback(hObject, eventdata, handles)
% hObject handle to openFile (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%== GUI get file in the folder ==%
[FileName,PathName] = uigetfile({'*.tif';'*.jpg';'*.png'},'Select a image file');
%== Create a object handles.img to load image ==%
handles.img = imread(FileName);
%== call axes whose tag is axes1 to show image ==%
axes(handles.axes1);
imshow(handles.img);
guidata(hObject, handles);
Next time you want to use any varable in GUI
Don't forget to check whether that object exists
Hope it helps
After loading an image into a Matlab GUI, how can I pass that image using pushbutton to another Matlab file? When I push the button in my GUI, the image should be passed to my Matlab code.
Here is my GUI code
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[filename,pathname]=uigetfile(...
{'*.jpg;*.gif;*.png;*.bmp',...
'image file(*.jpg,*.gif,*.png,*.bmp)';'*.*','all files(*.*)'},...
'open the image file to be verified');
fullimagefilename = fullfile(pathname,filename);
axes1 = imread(fullimagefilename);
axes(handles.axes1);
image(axes1);
%axes(handles.axes1);
%imshow('E:\degraded images\3.jpg')
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
-pushbutton 1 is used to browse file .
-and by pushbutton2 i want to pass the browsed file to my base file and its code is
im=imread('E:\degraded images\3.jpg');
hsv = rgb2hsv(im);
hueImage = hsv(:,:, 1);
meanHue = mean2(hueImage);
figure, imshow(im);
title(meanHue);
%figure , imshow(im);
%im = imresize(im, 0.5);
%im2 = imread('E:\degraded images\3.jpg') ;
im2= im;
im=im(:,:,1);
sigmaA=8;
sigmaB=10;
sigmaMax=max([sigmaA sigmaB]);
fsz=[sigmaMax, sigmaMax];
If I understand correctly, the GUI uses pushbutton1 to allow the user to choose a file and display it in the axes. When the user presses the other button, pushbutton2, which is presumably on the same GUI, you want the callback for that button to have access to the selected (or browsed) image.
This can be done by creating a field in the handles structure for the selected image data, then storing this data using the guidata function.
In your first callback, do something like the following
function pushbutton1_Callback(hObject, eventdata, handles)
[filename,pathname]=uigetfile(...
{'*.jpg;*.gif;*.png;*.bmp',...
'image file(*.jpg,*.gif,*.png,*.bmp)';'*.*','all files(*.*)'},...
'open the image file to be verified');
% need to handle the case where the user presses cancel
if filename~=0
fullimagefilename = fullfile(pathname,filename);
% create an img field in handles for the image that is being loaded
handles.img = imread(fullimagefilename);
axes(handles.axes1);
image(handles.img);
% save the application data
guidata(hObject,handles);
end
Now in your second callback, you can access this file through the handles struct
function pushbutton2_Callback(hObject, eventdata, handles)
% check to ensure that img exists in handles
if isfield(handles,'img')
% copy the image from the handles structure
im=handles.img;
% continue with your code
hsv = rgb2hsv(im);
% etc.
end
And that is it - the first callback saves the image to the handles structure as a field within it, and the second callback retrieves that image from handles via the img field.
Try it out and see what happens!
When I try to invoke a subfunction in a GUI/GUIDE file (using a function handle which has been exposed as a global variable), a new axes is always created even if I set the axes to a specific axes in the GUIDE figure. Does anyone know why this is happening? GUIDE code is:
###############################################################
function varargout = demo(varargin)
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #demo_OpeningFcn, ...
'gui_OutputFcn', #demo_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
function demo_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
global myhandles updateFunction;
myhandles = handles;
updateFunction = #update;
function varargout = demo_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
function pushbutton1_Callback(hObject, eventdata, handles)
update();
function update()
global myhandles;
axes(myhandles.axes1);
plot(1:2,1:2);
###########################################################################
And when I do (outside file above):
global updateFunction;
feval(updateFunction)
I always see the plot in a newly created figure window, not in the GUI figure. Why is this happening?
The first thing I would try is to replace the function update with the following:
function update
global myhandles;
plot(myhandles.axes1,1:2,1:2);
This will explicitly tell the PLOT function to plot into the given axes. If that doesn't work, try setting the axes 'NextPlot' property to 'add' (probably in demo_OpeningFcn):
set(myhandles.axes1,'NextPlot','add');
By default, when you create a GUI using GUIDE, Matlab sets the 'HandleVisibility' property of all objects associated with the GUI to 'callback'. This means that you cannot set these handles to be the current figure or current axis from anywhere outside of the callback routines (ie. from the command line or an external function).
To work around this you can either specify the appropriate handle explicitly in all of your plotting functions or you can set the 'HandleVisibility' property of the axes to 'on'. This can be done on an object by object basis via the property inspector, or for the entire GUI by going to 'Tools -> GUI Options...' and changing the 'Command-line Accessibility:' to 'on'.