Close MATLAB GUI with a dialog - matlab

I'm doing a GUI with GUIDE and want to close the figure with a dialog.
I have a button with this
selection = questdlg('Close This Figure?',...
'Close Request Function',...
'Yes','No','Yes');
switch selection,
case 'Yes',
delete(gcf)
case 'No'
return
end
and it works fine but I want the main button to do the same thing.
I tryed to put this next to
function varargout = file_name(varargin)
...
but won't work. Any suggestions?

Use above code in a function say:
%Save in myclose_callback.m
function myclose_callback(src,evnt)
selection = questdlg('Close This Figure?',...
'Close Request Function',...
'Yes','No','Yes');
switch selection,
case 'Yes',
delete(gcf)
case 'No'
return
end
end
Then use:
%From other .m script or from command window
figure('CloseRequestFcn',#myclose_callback)
To make this as your default callback for all figures, use:
set(0,'DefaultFigureCloseRequestFcn',#myclose_callback)

Related

Editing fields in matlab

I am creating message box in MATLAB with the following code
prompt={'Length'}
name = 'Input';
answer = inputdlg(prompt,name,[1 40],defaultans);
Length = str2double(answer{1});
choice = questdlg('Would you like to confirm?', ...
'Message Box', ...
'Yes','No','No');
switch choice
case 'Yes'
h = msgbox({'Operation' 'Completed'});
case 'No'
h = msgbox({'Operation' 'Failed'});
end
I am entering the value as shown in below image
After moving to next window, when i press 'No' , I want the same earlier Input window as shown above to be displayed with 120 written inside the input box so that i can change the value.
Can anyone please let me know how to switch to previous window wherein i can edit my values which have been earlier written.
Use an infinite while loop and put the inputdlg statement inside it. When the user confirms, break it.
Modified Code:
prompt={'Length'};
name = 'Input';
defaultans={'120'};
while 1
answer = inputdlg(prompt,name,[1 40],defaultans);
choice = questdlg('Would you like to confirm?', ...
'Message Box', ...
'Yes','No','No');
switch choice
case 'Yes'
h = msgbox({'Operation' 'Completed'});
Length = str2double(answer{1});
break;
end
end

Can waitforbuttonpress be used with switch cases

I have used this piece of Code but it's not working. Can you help me where I am wrong here..
for i=1:10
keydown = waitforbuttonpress;
switch keydown
case'0'
disp(5);
case'1'
disp(6);
end
end
Thank you
You are using '0' and '1', when you should use 0 and 1.
'0' - char type '0' (ASCII value of '0' equals 48. double('0') = 48).
waitforbuttonpress documentation:
k = waitforbuttonpress blocks the caller's execution stream until the function detects that the user has clicked a mouse button or pressed a key while the figure window is active. The figure that is current when you call the waitforbuttonpress function is the only area in which users can press a key or click a mouse button to resume program execution. The return argument, k, can have these values:
0 if it detects a mouse button click
1 if it detects a key press
Modify your code as follows:
for i=1:10
keydown = waitforbuttonpress;
switch keydown
case 0
disp(5);
case 1
disp(6);
end
end

MATLAB GUI - bring up a string

i' m try to create a simplex and fast gui on matlab. I made a button with a callback function, but I don' t know how to bring up a string that depends upon the result of the function. For example if the result of the function is 1, must appear the string "All ok!", if the result of the function is 0, must appear the string "It' s wrong!!!".
You can create a Static Text object or an inactive Edit Text object and modify the String property from your script with:
set(handles.edit_text, 'String', 'your_string')
An alternative to the static/edit uicontrol is to use a msgbox or an errordlg to display the result to the user depending on the result of your function.
if myFunction
msgbox ( 'All Ok' );
else
errordlg ( 'Error in Function', 'Error' )
end

MATLAB GUI Button with Keyboard Control

I have a button in a GUI. which the user presses to execute a Callback. But, I would like the user to be able to press the up arrow key instead of clicking to execute the callback.
EDIT: I am using GUIDE to make the GUI
Check out this thread:
http://www.mathworks.com/matlabcentral/answers/12034
Just slightly modifying the code from there to here (put the following in a file called testGUI.m
function testGUI
g = figure('KeyPressFcn', #keyPress)
MyButton = uicontrol('Style', 'pushbutton','Callback',#task);
function task(src, e)
disp('button press');
end
function keyPress(src, e)
switch e.Key
case 'uparrow'
task(MyButton, []);
end
end
end

Get the handle of the contex menu caller

In matlab if I've a context menu with handle cxmenu_Options which is linked to different three uicontrol objects.
Inside the context menu callback function:
Code Demo:
function demoOnContextMenus
hFigure = figure;
hControl = uicontrol( ...
'Parent' , hFigure , ...
'Style' , 'Edit' , ...
'Position' , [200 200 180 40] , ...
'Tag' , 'IamControl' , ...
'String' , 'UI-Control');
hCxMenu = uicontextmenu( ...
'Tag' , 'IamMenu' , ...
'Callback',#CxMenuCallback);
set(hControl,'UIContextMenu',hCxMenu);
function CxMenuCallback(objectHandle,eventData)
tag = get(gcbo,'tag');
helpdlg(tag);
end
end
How Can I get the handle of the uicontrol which the context menu has been called from ?
There are two ways to access the handle:
gco returns the handle of the currently selected object. Thus tag = get(gco,'tag') will return IamControl.
Alternatively, you can pass the handle directly to the callback (in case the hierarchy becomes more complicated, since gco will only give you the top-level handle of the eventual chain):
handleToPass = hControl;
hCxMenu = uicontextmenu( ...
'Tag' , 'IamMenu' , ...
'Callback',#(oh,evt)CxMenuCallback(oh,evt,handleToPass));
set(hControl,'UIContextMenu',hCxMenu);
function CxMenuCallback(objectHandle,eventData,handleOfCaller)
tag = get(handleOfCaller,'tag');
helpdlg(tag);
end
Using Matlab's guide environment I have found another way to determine the caller.
The command gco (get current object) simply did the job.
In my case the context menu provides the option to open a path specified in an "Edit Text" object in Windows Explorer.
function open_in_browser_Callback(hObject, eventdata, handles)
cur_obj=gco;
cur_path=get(cur_obj,'String')
if(~isempty(cur_path))
winopen(cur_path);
end
With this solution I was able to use the same context menu for two "Edit Text" objects.