Simple pushButton with changing text in MATLAB - matlab

I am trying to implement a very simple GUI that consists of just one pushButton. I want it to begin by just having START as a label. Then on press it changes to STOP. When the user clicks the button the first time the callback sets a boolean to true and changes the label. When the Button is clicked a second time the boolean is changed to false and the GUI closes.
I can't find anything on how to make a simple GUI like this in MATLAB. the GUIDE tool makes no sense to me and seems to generate so much useless code. Matlab buttons are wrappers for jButtons as seen here

GUIDE is quite straightforward - the automated tool generates stubs for all the callbacks, so that all is left is to fill in the code to be executed whenever the callback runs. If you prefer to create the GUI programmatically, you can create the button you want as follows:
%# create GUI figure - could set plenty of options here, of course
guiFig = figure;
%# create callback that stores the state in UserData, and picks from
%# one of two choices
choices = {'start','stop'};
cbFunc = #(hObject,eventdata)set(hObject,'UserData',~get(hObject,'UserData'),...
'string',choices{1+get(hObject,'UserData')});
%# create the button
uicontrol('parent',guiFig,'style','pushbutton',...
'string','start','callback',cbFunc,'UserData',true,...
'units','normalized','position',[0.4 0.4 0.2 0.2])

Related

Matlab Get Handle for Layout

As the title states, I need to get a handle for my Matlab application. My class is derived from matlab.apps.AppBase and is app.UIFigure (if that matter, I'm still learning Matlab). My main goal is to change the mouse cursor to watch after a button is clicked and data is processed in the background.
I have tried:
set(gcf,'Pointer','watch')
But gcf is just empty, so it creates a new figure. I have also gotten all of the figures, using:
figs = findall(groot,'Type','Figure')
Which finds all of the figures I am using. I believe that I need to get the overall application figure and find the handle, but I am unsure how to do that.
There is no pointer property for uifigure; otherwise, you would be able to use app.UIFigure.Pointer = 'watch' as suggested by #CrisLuengo.
However, specially for uifigure MATLAB provides a nice looking and powerful progress bar uiprogressdlg. You can make it indeterminate with uiprogressdlg.Indeterminate = on;. I find this working pleasingly well.
Here is an example:
f=uifigure;
progressdlg=uiprogressdlg(f,'Title','Progress','Message', 'Doing something please wait', 'Indeterminate','on');
pause(10); % Run your algorithm.
% Delete the progress bar after work done.
progressdlg.delete();

Conditional pausing in Matlab (not Debugging)

I am new to Matlab so please bear with me.
So I created a two GUID GUI in which one generates a dynamic data and updates the plotting every second so I used pause(1) (which is continuous) and the second one takes the full data of the first GUI and plots it (opens on button press)
Is there a way where if I open the the second GUI the first GUI pauses and if and only if the second GUI is stopped the first GUI resumes its process?
Thanks in advance.
Update
Example:
gui1.m
function guie1()
for ii=1:100
c = magic(ii)
plot(c);
% a button at this point
% Some pause condition
drawnow;
end
so when I click on that button it would open a window (a new figure may be) so unless I close is the loop should be paused.
Here is the example:
run(fullfile(docroot,'techdoc','creating_guis','examples','callback_interrupt'));
Here is the link:
http://www.mathworks.com/help/matlab/creating_guis/callback-sequencing-and-interruption.html
Update:
Here:
http://blogs.mathworks.com/videos/2010/12/03/how-to-loop-until-a-button-is-pushed-in-matlab/
http://www.mathworks.com/matlabcentral/fileexchange/29618-spspj

Matlab update plot in one GUI based on activity in second GUI

In one GUI (viewer) I have an image that shows a 2D slice through a 3D image cube. A toolbar button opens a second GUI (z-profile) that plots a 2D graph showing the z-profile of one pixel in the image cube. What I want is to be able to update this plot dynamically when a different pixel is clicked in the original viewer GUI. I've looked in to linkdata but I'm not sure if that can be used to link across two GUIs. Is there a simple way to do this without re-creating the second GUI each time a new pixel is clicked and feeding in the new input location?
You can definitely doing it without recreating the second GUI every time.
Without knowing your specific code I would say that you should store a reference to the second GUI in the first GUI, then in a callback for clicking a pixel in the first GUI, change data in the second GUI via the stored reference (e.g. figure handle). You can store arbitrary data in a figure, for example by using function guidata. A bit of code.
...
figure2 = figure();
figure1 = figure('WindowButtonDownFcn',#myCallback);
guidata(figure1, figure2);
...
function myCallback(obj,eventdata)
figure2 = guidata(obj);
...
Even easier but a bit more error-prone would be to use global variables for storing the references.

Two figures in two different files - how to run first fig from the second one?

I have two figs in two different files.
By clicking a button on first fig I want to show the second one... how to do this? is it possible?
If YES than how to exchange with data between two figures?
There are a number of ways to share data among GUIs. In general, you need to somehow make the graphics handle(s) from one GUI available to the other GUI so it can get/set certain object properties. Here's a very simple example that involves one GUI creating another and passing it an object handle:
function gui_one
hFigure = figure('Pos',[200 200 120 70],... %# Make a new figure
'MenuBar','none');
hEdit = uicontrol('Style','edit',... %# Make an editable text box
'Parent',hFigure,...
'Pos',[10 45 100 15]);
hButton = uicontrol('Style','push',... %# Make a push button
'Parent',hFigure,...
'Pos',[10 10 100 25],...
'String','Open new figure',...
'Callback',#open_gui_two);
%#---Nested functions below---
function open_gui_two(hObject,eventData)
gui_two(hEdit); %# Pass handle of editable text box to gui_two
end
end
%#---Subfunctions below---
function gui_two(hEdit)
displayStr = get(hEdit,'String'); %# Get the editable text from gui_one
set(hEdit,'String',''); %# Clear the editable text from gui_one
hFigure = figure('Pos',[400 200 120 70],... %# Make a new figure
'MenuBar','none');
hText = uicontrol('Style','text',... %# Make a static text box
'Parent',hFigure,...
'Pos',[10 27 100 15],...
'String',displayStr);
end
After saving the above code to an m-file, you can create the first GUI by typing gui_one. You will see a small figure window with an editable text box and a button. If you type something in the text box, then hit the button, a second GUI will appear next to it. This second GUI uses the handle to the editable text box that is passed to it from the first GUI to get the text string, display it, and clear the string from the first GUI.
This is just a simple example. For more information on programming GUIs in MATLAB, take a look at the MathWorks online documentation as well as the links in the answers to this SO question.

Matlab IMRECT backward compatibility

I've writtend a GUI function in MATLAB R2009b which makes use of the IMRECT function. I need to make sure this GUI also works in MATLAB R2007b: since this release the IMRECT function has undergone extensive changes. I have two question:
1 - in the new (R2009b) IMRECT, a method GETCOLOR is defined which allows to get the color which was selected by the user using the scroll menu. Is there a way to mimic this behavior for the old (R2007b) function?
2 - in MATLAB R2009b I can use WAIT after using IMRECT as follows:
h = imrect(axhandle);
wait(h);
this allows to wait unitl the user as correctly placed his/her rectangle and has double click to confirm the choice. Is there anything analogous that can be used with IMRECT from R2007b?
Unfortunately, you need a workaround for both functions.
Here is one way to do it:
%# Create a figure and some points
fh = figure;plot(rand(10,1),rand(10,1),'.')
ah = gca;
%# this allows the user to place the rectangle. However, the code resumes
%# as soon as the rectangle has been drawn
rh = imrect(ah,[]);
%# Create a dialog to have the possibility to uiwait
wh = warndlg('Please close this dialog once you are done adjusting your rectangle');
uiwait(wh)
%# Get the color of the rectangle
rectKids = get(rh,'Children');
rectangleColor = get(rectKids(1),'Color');
You can use verLessThan to check for the Matlab version in order to get the proper functionality. However, if there are users who'll use the code both on 2007b and 2009b, I suggest you leave the dialog box in for everyone, so that they don't get confused when they switch.