matlab gui becomes unresponsive or partially closes - matlab

I am currently working on a matlab gui and after some beginner's problems with the data handling I am quite satisfied with the result.
There is just one hiccup: whenever the program is done running, the gui becomes unresponsive and the buttons and text elements vanish, all I can see is the background.
I have scanned the functions thoroughly for close all; statements and such, but there is nothing there.
How do I return 'clean' to the gui so I can put in more data? Do I need to put the gui in a constant while loop?
best wishes
Chris

You can do the following:
Modify the attribute of your controls to be interruptible:
set(handles.figure, 'Interruptible','on');
Create a callback function based on pressing a determined key combination.
set(KeyPressFcn, #resume_fcn);
Create a callback function that solves the problem.
function resume_fcn()
if eventdata.Key = ...
exit;
end
end
However, the consistency of data may be lost. In case you don't want to return 'clean' to the gui, you can type:
delete(get(0,'Children'))

Related

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);

Check if editbox is active (and evaluate WindowKeyPressFcn only if its not) in matlab

I would like to be able to test whether given edit box (uicontrol with 'style' set to 'edit') is being used at the moment (whether the cursor is inside that box / the edit box has focus).
Unfortunatelly the 'Selected' property of the editbox seems to be 'off' all the time and it's use is discouraged by MathWorks
I am open to Java solutions (but I have not tried any yet).
Background
My broader goal is to activate specific functions in response to keys pressed by the user (keyboard shortcuts) but only if the user is not typing these letters into an edit box.
I evaluate keyboard shortcuts with 'WindowKeyPressFcn' callback. I thought about using editbox's 'KeyPressFcn' callback to block 'WindowKeyPressFcn' (by passing something through setappdata or 'UserData') but it seems that 'WindowKeyPressFcn' is always evaluated first.
So now I'm trying to detect whether the editbox is in use some other way.
Since MATLAB uicontrols have underlying Java Swing GUI controls, I can think of two "Java solutions":
Associate a FocusGainedCallback with the Java object of editbox (using FindJObj - see below) (and possibly other UI elements), and update some handle \ persistent \ global variable from this function to hold the currently focused object handle. This type of callback is mentioned in an UndocumentMatlab article on uicontrol callbacks.
You should be able to use Java'sisFocusOwner() method on the underlying java object of your editbox (this method is mentioned in the Java documentation of the focus subsystem).
Note: the simplest way to get the java object is using the utility FindJObj that is briefly described here.
An alternative, much simpler method, would be to use the WindowKeyReleaseFcn callback of the GUI figure. This gets fired after the release of the keystroke, hence it gets executed after the gco gets the updated value of the currently focused uicontrol. The following check in the beginning of the WindowKeyReleaseFcn can determine if the keystroke comes from the edit box:
function figure1_WindowKeyReleaseFcn(hObject, eventdata, handles)
% gco is already updated by the time we are here to reflect the uicontrol
% which is in focus
if isequal(gco, handles.hEditBox) % Keystroke comes from the edit box
return
end
% Keystroke does not come from the edit box -- Do something here...

How do I execute a function that receives handles as an argument, in another GUI?

I'm trying to change my plot in my main GUI according to a different number of options that I've put in another GUI. So, the main objective of this is to press the "OK" button on my second GUI, and that automatically changes the plot of the main one. I've already defined a function that does all that in my main GUI, and I wanted to use it on my second one.
Basically, I'm trying to use a function from one GUI to another and that function receives handles as its only argument.
Here's a bit of what I want
Main GUI - plotGraphs(handles) - This is the function in my main GUI that plots a graphic, according to several defined global variables.
Second GUI - Changes some of those global variables. Pressing "OK", the function plotGraphs is called and should change the Main GUI, according to the new variables.
I just don't know how to do this. I've tried putting this into the "OK" button callback
plotGraphs(handles)
close(secondGUI);
And it does not work. I've also tried passing the handles from the Main GUI to the second, but that does not seem to work either, or maybe I'm doing something wrong. Also, I'm using GUIDE for my GUI.

Determining direct-feedthrough paths without compilation/execution

I am currently working on a tool written in M-Script that executes a set of checks on a given simulink model. This tool does not compile/execute the model, I'm using find_system and get_param to retrieve all the information I need in order to run the routines of my tool.
I've reached a point where I need to determine whether a certain block has direct-feedthrough or not. I am not entirely sure how to do this. Two possible solutions come to mind:
A property might store this information and might be accessible via get_param. After investigating this, I could not find any such property.
Some block types have direct-feedthrough (Sum, Logic, ...), some other do not (Unit Delay, Integrator), so I could use the block type to determine whether a block has direct-feedthrough or not. Since I'm not an experienced Simulink modeller, I'm not sure if its possible to tell whether a block has direct-feedthrough by solely looking at its block type. Also, this would require a lookup table including all Simulink block types. An impossible task, since additional block types might get added to Simulink via third party modules.
Any help or pointers to possible solutions are greatly appreciated.
after some further research...
There is an "official solution" by Matlab:
just download the linked m-file
It shows that my idea was not that bad ;)
and for the record, my idea:
I think it's doable quite easily. I cannot present you some code yet, but I'll see what I can do. My idea is the following:
programatically create a new model
Add a Constant source block and a Terminator
add the Block you want to get to know the direct feedthrough ability in the middle
add_lines
run the simulation and log the states, which will give you the xout variable in the workspace.
If there is direct feedthrough the vector is empty, otherwise not.
probably you need to include some try/catch error catching for special cases
This way you can analyse a block for direct feedthrough by just migrating it to another model, without compiling your actual main model. It's not the fastest solution, but I can not imagine that performance matters that much for you.
Here we go, this script works fine for my examples:
function feedthrough = hasfeedthrough( input )
% get block path
blockinfo = find_system('simulink','Name',input);
blockpath = blockinfo{1};
% create new system
new_system('feed');
open_system('feed');
% add test model elements
src = add_block('simulink/Sources/Constant','feed/Constant');
src_ports = get_param(src,'PortHandles');
src_out = src_ports.Outport;
dest = add_block('simulink/Sinks/To Workspace','feed/simout');
dest_ports = get_param(dest,'PortHandles');
dest_in = dest_ports.Inport;
test = add_block(blockpath,'feed/test');
test_ports = get_param(test,'PortHandles');
test_in = test_ports.Inport;
test_out = test_ports.Outport;
add_line('feed',src_out,test_in);
add_line('feed',test_out,dest_in);
% setup simulation
set_param('feed','StopTime','0.1');
set_param('feed','Solver','ode3');
set_param('feed','FixedStep','0.05');
set_param('feed','SaveState','on');
% run simulation and get states
sim('feed');
% if condition for blocks like state space
feedthrough = isempty(xout);
if ~feedthrough
a = simout.data;
if ~any(a == xout);
feedthrough = ~feedthrough;
end
end
delete system
close_system('feed',1)
delete('feed');
end
When enter for example 'Gain' it will return 1, when you enter 'Integrator' it will return 0.
Execution time on my ancient machine is 1.3sec, not that bad.
Things you probably still have to do:
add another parameter, to define whether the block is continuous or discrete time and set the solver accordingly.
test some "extraordinary" blocks, maybe it's not working for everything. Also I haven implemented anything which could deal with logic, but actually the constant is 1 so it should work as well.
Just try out everything, at least it's a good base for you to work on.
A famous exception is the StateSpace Block which can have direct feedthrough AND states. But there are not sooo much standard blocks with this "behaviour". If you also have to deal with third party blocks you could get into some trouble, I have to admit that.
possible solution for the state space: if one compares xout with yout than one can find another indicator for direct feedthrough: if there is, the vectors are not equal. If so, than they are equal. Just an example, but I can imagine that it is possible to find more general ways to test things like that.
besides the added simout block above one needs the condition:
% if condition for blocks like state space
feedthrough = isempty(xout);
if ~feedthrough
a = simout.data;
if ~any(a == xout);
feedthrough = ~feedthrough;
end
end
From the documentation:
Tip
To determine if a block has direct feedthrough:
Double-click the
block. The block parameter dialog box opens.
Click the Help button in
the block parameter dialog box. The block reference page opens.
Scroll
to the Characteristics section of the block reference page, which
lists whether or not that block has direct feedthrough.
I couldn't find a programmatic equivalent though...
Based on a similar approach to the one by #thewaywewalk, you could set up a temporary model that contains an algebraic loop, similar to,
(Note that you would replace the State-Space block with any block that you want to test.)
Then set the diagnostics to error out if there is an algebraic loop,
If an error occurs when the model is compiled
>> modelname([],[],[],'compile');
(and you should check that it is the Algebraic Loop error that has occured), then the block has direct feed though.
If no error occurs then the block does not have direct feed though.
At this point you would need to terminate the model using
>> modelname([],[],[],'term');
If the block has multiple inports or outprts then you'll need to iterate over all combinations of them.

Avoid interruption of callback functions in Matlab GUI

I have a Matlab GUI that needs a high time to execute some callback functions. Besides, these functions include the following code:
drawnow('expose');
pause(handles.data.delay);
I want to avoid that those callback executions get interrupted in order to avoid data inconsistency if the user presses other buttons. Thus, I modify the figure settings as:
set(handles.figure, 'BusyAction','cancel', 'Interruptible','off');
However, the callbacks are still interrupted. How can I avoid it?
Note: I think that the problem is that I need to propagate the 'BusyAction' and 'Interruptible' values to all the controls in my GUI, is there any way to do it automatically? Like, for example, modifying the default value before generating the GUI.
The fastest and cleanest way to propagate any property to all UI objects is with findobj:
set(findobj('Type','uicontrol'), 'BusyAction','cancel', 'Interruptible','off');