MATLAB - callback excecution of edit box - matlab

As it is referenced in MATLAB documentation for edit box uicontrol or stated in this
post, when another component or menu bar or background GUI is clicked, the edit box callback gets executed. But in my attempts to use this functionality, I haven't been able so far to see the callback execution unless there is a change of edit box text or Enter key is pressed. What I'm trying to achieve is to execute edit box callback whenever there is focus loss from edit box even when nothing has been entered. Please enlighten me about what I'm missing here and how I can do this?
Thanks in advance.

The underlying Java object has a callback called FocusLostCallback that'll do what you want - execute when the object's focus is lost, even if you changed nothing.
You'll need findjobj from the MATLAB File Exchange. Then, get the Java handle and set the callback as usual (make sure the uicontrol is visible when you try to get the Java handle):
jh = findjobj(myEditBox); % myEditBox is a uicontrol handle
set(jh, 'FocusLostCallback', #myCallback);
A more complete list of the undocumented uicontrol callbacks can be found at Yair Altman's Undocumented MATLAB blog.

This method work perfectly with single-line textbox, but it has any effect with multi-line textbox (uicontrol, style edit, max = 2)

Related

Programmatic Method of Handling Matlab Dialog Windows

I have a rather large Matlab program that is GUI based. I am looking into creating automated tests for it, as the current way of checking for bugs before a release is simply using all its functionality like a user would.
I would rather not use a GUI testing program that just records clicks and what not, so I was thinking of adding testing code that would call the button callbacks directly. The problem that I have run into with this is that we have a lot of warndlg and msgbox popups, and I would like my tester code to be able to see these.
Is there any way for Matlab code to tell if a function it called created a warndlg or msgbox? If so, is there any way to click 'ok' on these popups?
In a similar vein, is it possible to handle popups that block code execution (using uiwait or an inputdlg)?
If it matters I didn't use GUIDE, and all the GUI elements are created programmatically
Two ways. The first one is more elegant
Let the functions return an extra variable and return the status of the function. For example, 1: success, 2: success with warning, 3: error...
Create some global variables and make the function change them if a warndlg or msbgbox shows up. The main window would then check if the status of the global variable.
You can tell if a warning dialog was created by looking for it's tag using the findobj function. A warning dialog created using warndlg will have the tag "Msgbox_Warning Dialog". So code like this would tell you if the warning dialog exists:
set(0,'ShowHiddenHandles', 'on')
h = findobj('Tag', 'Msgbox_Warning Dialog');
warn_exists = ~isempty(h)
set(0,'ShowHiddenHandles', 'off')
to close the warning dialog, you can call delete, like this:
delete(h)
For the message box, I would store the handle when you create a message box, then look at the children to find the buttons, then look at their callbacks. You should be able to call the callbacks to simulate picking a button.

How to block GUI while function with output executes?

I am creating a MATLAB application in GUIDE and now I'm facing a problem. I need to call a function that takes a long time to execute and returns a value but while executing the function I want the GUI to wait for the returned value.
I tried with waitfor but this way I can still interact with the GUI and I can't take the returned value...
waitfor(function);
I can think of something that disables all the GUI then enables it back but I have both enabled and disabled objects...
Do you know any solution to this problem?
A simple solution is to create a modal dialog box with a message "Please wait..." just before executing your long-running function, and then to close the dialog box just after it completes. A modal dialog will be in front of the GUI, and will not allow interactions with the window behind.
It's possible for the user to click the "Close" button on the dialog, but you can override this by setting the "CloseRequestFcn" property of the dialog, so that the close button does nothing (unfortunately you can't easily hide the button).
I like the modal dialog proposed by Sam Roberts. There is no mystery and it is user friendly.
Another dirty and easy solution may be to hide the GUI completely, if it is okay:
set(hFig, 'Visible', 'off');
And set it to 'on' after done. It will be good practice to make sure to set it to 'on' in catch block, to avoid disappearing GUI due to error during execution.

Enabling and Disabling GUI button Matlab

This is a very strange problem, because I am quite sure the logic is proper but it is still not working. So when the GUI starts, in the starting function, I set most of the buttons to be inactivated using the following line of code for every button, Kbutton, Bbutton etc..:
set(handles.Kbutton,'Enable','off');
set(handles.Bbutton,'Enable','off');
Then, when the user clicks on any cell in the uitable, I use the cell selection callback to get the info about the contents of the cell, and re-activate the buttons that can be used from then on:
set(handles.Kbutton,'Enable','on');
set(handles.Bbutton,'Enable','on');
And till now everything is ok.
So then the user clicks on a button, say Kbutton, and the underlying function is executed thanks to the button's callback function. Once the method is ready I would like to return the buttons to their initial state i.e. inactivated. So logic tells me, re-put the initial line of code at the end of the callback function and done:
set(hObject,'Enable','off');
drawnow;
set(handles.BButton,'Enable','off');
drawnow;
Where the first line of code in the above snippet, refers to the button who fired the callback and BButton is any other button in the GUI.
For some strange reason only the second one is being enabled off. I.e. the button that fired the callback is staying enabled on :/
Has anyone encountered the same problem?
Any ideas?
Thanks in advance
Try changing
set(hObject,'Enable','off');
to
set(handles.Kbutton,'Enable','off');
You click on tools =>gui options =>Generate FlG file and MATLAB file

How to call a Dialogue box from Menu using Matlab?

I want to Make a Help Dialogue Box. When we Press Help Menu then Dialogue box will appear.
Current code is
h_opt3 = uimenu('Label','&Help');
uimenu(h_opt3,'Label','How to Use','Callback','dialog','separator','on');
It only show empty dialog.
In place of dialog i want to put a dialogbox which will describe how to use my software.
How to code this all. Please help.
Requirements: How to Add stuff in DialogBox and how to call it in above mention scenario.
Try writing an additional callback function, and look at helpdlg instead of dialog.
I can't test any code now, but something like the following should work.
function help_callback
h = helpdlg('Directions','title');
end
Then change your uimenu call to this code
uimenu(h_opt3,'Label','How to Use','Callback',#help_callback,'separator','on');

How to implement a button into Simulink Subsystem Mask?

As I need to specify a local variable to a Subsystem, I created a mask. Doing that I lose the easy access to the subsystem. Right-click and navigating to "Look under mask" is supposed to be too complicated.
So I thought about a workaround and built the following:
The dialog callback code behind the "Get deeper!" checkbox is:
myParameter = %Parameter set by checking Get deeper!
path = gcb(gcs);
if strcmp(get_param(gcb,'myParameter'),'on')
open_system(path,'tab');
end
Everytime when I check the box, the subsystem gets opened and also by every double click on the subsystem, in case the box was checked before. Hence the code does what it should, but thats actualy not the common way how one would realize/visualize something like this.
What I want is a button "Look under mask" in my mask - so the subsystem just gets opened by clicking on that button. Basically the button should call the function: open_system(gcb(gcs),'tab'). Looks so easy, but Simulink doesn't offer me any option to implement this. Can anybody help?
The main issue whith the current solution is also that with every execution of the model all subsystems open up, where the box is checked. That's not the idea.
Matlab 2012b adds exactly what you want: masked blocks have a button on the botton left that is a shortcut to "Look under mask".
Unfortunately, I don't think it is possible to add a button in a mask.
You may want to change your function to automatically set the "Get deeper!" checkbox off after the user clicks on it. That would avoid the automatic opening of the subsystems when the model is loaded. You could do that adding set_param(path,'myParameter','off') just after the open_system(path,'tab');
Finally, as another workaround, you may want to set the OpenFcn callback to call open_system(gcb,'tab'). This will make the system work as if it isn't masked at all. You can put two open_system calls, one to look under mask and the other to open the mask dialog box, if you prefer.