How to make a GUI pushbutton open/close another GUI - matlab

I have a GUI called GUI_main in which I have a pushbutton called pushbutton_GUI_main. I currently have the following callback function implemented:
function pushbutton_GUI_main_Callback(hObject, eventdata, handles)
GUI_sub
Where GUI_sub is another GUI that opens when you click on pushbutton_GUI_main. However, I want something like the following:
function pushbutton_GUI_main_Callback(hObject, eventdata, handles)
if (GUI_sub == open)
close(GUI_sub)
else
GUI_sub
That is, with pushbutton_GUI_main I want to be able to open and close GUI_sub.

You need an object handle to reference the sub GUI. Assuming GUI_sub is a GUI built with GUIDE, it is programmed by default with an optional handle output.
A naive implementation for a GUIDE GUI would look something like this:
function pushbutton1_Callback(hObject, eventdata, handles)
if ~isempty(handles.figure1.UserData)
close(handles.figure1.UserData);
handles.figure1.UserData = [];
else
handles.figure1.UserData = sub_GUI;
end
Most of (maybe all?) MATLAB's graphics objects have a UserData field by default. I've utilized the UserData of the base figure object for this simple example. See also: Share Data Among Callbacks for other methods to store/transfer this data.

As excaza says, handles is a great way to pass data or information in a GUI.
Another way, if you for some reason don't want to store the GUI handle, perhaps if the GUI_sub could be created independently is to search for the figure handle.
subGuiH = findall(0,'Name','GUI_sub');
if ~isempty(subGuiH)
close(subGuiH);
end
GUI_sub;
The search could be narrowed by adding
findall(0,'Type','figure','Name','GUI_sub')
Depending on your Matlab version, you could also checkout groot

Related

Call GUI function from another GUI

I have GUI1 that opens GUI2.
I want GUI2 to execute a function from GUI1.
This link shows how to execute a callback, but i want to execute a function.
Thanks
You need to create a handle to that function and save it somewhere where it can be assessed.
e.g. in the GUI1 opening function (where hObject is GUI1 figure object/handle):
func.myFun1=#myFun1;
func.myFun2=#myFun2;
setappdata(hObject,'fun_handles',func);
then, in GUI2, you can recover the handles with func=getappdata(GUI1_figure,'fun_handles');, where GUI1_figure is GUI1 figure object. The figure object can be either saved when the GUI is greated (e.g. if GUI2 is always created from GUI1, pass the object as an argument and save it somewhere) or found with something like findobj(0,'-depth',1,'Tag','tag_of_GUI1_figure')

In Matlab, how to call GUI callback functions without the GUI?

I am not a GUI programmer, as will become obvious. If anything, I'm trying to unmake a GUI. I am using a program called art (found here, if it's useful to see) that generates figures and variables that I would like to save. You can call art from a batch script and make it read a config file for its inputs, which is what I'm doing, but you have to manually generate and save much of its output (variables and figures) in the GUI. I'd love to automate this process, but I am really struggling.
I think the core of my issue would be solved if I knew how to force the callback functions to be called. For instance, there is a function in art showCorr_Callback(hObject, eventdata, handles) (which is governed by a radio button in the GUI). It has a test condition to execute:
if (get(handles.showCorr,'Value') == get(handles.showCorr,'Max'))
I've tried inserting
mx = get(handles.showCorr,'Max'))
setappdata(handles.showCorr,'Value', mx)
into a function I know executes, the opening function, function art_OpeningFcn(hObject, eventdata, handles, varargin). That doesn't seem to have any effect. If I knew how to make the callback functions execute, perhaps I could insert the code that saves the figure into the functions. Somewhere in Matlab's GUI scripts there must be something that is continually testing for a change in state in the GUI. Where is that thing? How can I fool it into thinking a radio button has been pressed?
Thanks for your help, and please let me know if I need to provide more information.
First of all, if you want to set the Value of handles.showCorr, you won't use setappdata as that just stores arbitrary data (by key/value pair) into the graphics object. You actually want to set the Value property.
set(handles.showCorr, 'Value', get(handles.showCorr, 'Max'))
This should trigger any callbacks that are assigned to handles.showCorr.
If, for some reason this doesn't trigger the callback, you can always trigger it manually. If you already know the callback, you can call it explicitly.
showCorr_Callback(hObject, eventdata, handles);

Can't access handles from a function

I'm trying to create a GUI using GUIDE, which reads a string through serial communication. After that it cuts out the needed numbers and puts it on the screen. I have created this function, which is executed every time when there is a line of data in the buffer of the COM port:
function out = intcon1(hObject, eventdata, handles)
global comPort;
a=fgetl(comPort);
disp(a);
a(a==' ') = '';
indexstart=strfind(a,'[');
indexend=strfind(a,']');
measureddata=a(indexstart(1):indexend(1));
commas=strfind(measureddata,',');
re1data=measureddata(2:(commas(1)-1));
re2data=measureddata((commas(1)+1):(commas(2)-1));
im1data=measureddata((commas(2)+1):(commas(3)-1));
im2data=measureddata((commas(3)+1):(commas(4)-1));
temp1data=measureddata((commas(4)+1):(commas(5)-1));
temp2data=measureddata((commas(5)+1):(commas(6)-1));
old_str=get(handle.re1, 'String');
new_str=strvcat(old_str, re1data);
set(handles.listbox8, 'String', re1data);
Now I'm trying to put the data into a listbox. This is just the first value. The problem is, that Matlab keeps saying, that the handles are not defined. But I cound already create a button which clears the listbox using the following code:
function clearlists_Callback(hObject, eventdata, handles)
% hObject handle to clearlists (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.listbox8, 'String', '');
Does anyone have any idea what the problem is and how to fix it?
Serial port callbacks are different to GUIDE callbacks. In the case of a serial port callback your object handle is to the serial port object and the event is a serial event. There is no third argument, hence handles being undefined.
If you want to retrieve your GUI handles from within this function you'll need to do so explicitly, similar to the way you're retrieving the comport handle - incidentally getting comport this way is probably unnecessary since I'd imagine it's the same object the callback is already receiving as hObject.
Since in this case handles is GUIDE-specific data, the 'proper' way to retrieve it
would be:
handles = guidata(gcf);
If your GUI has multiple figures, you may need to use findobj() instead of gcf() to get the right one.
You probably defined your BytesAvailableFCN using function handle syntax without additional arguments, like this
s.BytesAvailableFCN = #myfun();
Instead, you need to define your callback using a cell array, as explained here in the documentation. For example,
s.BytesAvailableFCN = {'myFun', handles};
handles must already be defined and in your workspace when that line is run.

GUIDE in MATLAB

I am trying to build a program in MATLAB and I am using edit boxes but I want the value that the user will enter to be used later on from another function. So should I use the global variables or is there another way?
I tried to define the global variables outside the function but is not working.
I tried to define it inside the function and then call it from another function but it says that it is undefined. Is there a way that I can do that?
I am just using
function edit1_Callback(hObject, eventdata, handles)
str2double (get (hObject,'String'));
Thanks!! :)
If you want to store data within a Matlab-GUI, you can use the handles-structure like this:
handles.myVar=123;
%after this dont forget to save it (yes, this is a bit annoying):
guidata(hObject, handles);
later on, within another callback for example, you can find this data within the handles-struct:
handles.myVar

Storage of gui data fails

I save and load gui data as described in the manual using
function readImage(filename, hObject, handles)
handles.image.data = imageRGBNoEdge;
guidata(hObject,handles);
and
function createHistogram(handles)
imageRGB = handles.image.data;
which are both called directly after the other
readImage(imageFile,hObject,handles);
createHistogram(handles);
However in he second function handles.image is unkown.
??? Reference to non-existent field 'image'.
Error in ==> ui_histogram>createHistogram at 252
imageRGB = handles.image.data;
But if I call the function a second time it is known?
Despite its name, handles is a structure and not a handle class so changing it within readImage does not propagate the change to the calling function.
You could either change readImage to be
function handles = readImage(filename, hObject, handles)
and call it with
handles = readImage(imageFile,hObject,handles);
or add a call to guidata
handles = guidata(hObject);
just before your calll to createHistogram.
I'm working off the assumption that imageRGBNoEdge is already defined at this point.
By briefly looking over the guidata function documentation it looks like that could be part of the issue here. When you try to load the data within createHistogram you need to be using something like this: imageRGB = guidata(hObject) because that is what tells guidata what data to get for you and you've already saved the data to that object handle (hObject)
Also, I can't really comment much more because it looks like there is also a fair bit of missing code for each of those functions. Hopefully this will help get you going in the right direction though!