access matlab figures by opening them with a mouse while a matlab terminal application is still running

7 vues (au cours des 30 derniers jours)
So i have a deployable terminal application that is a utility sent to colleagues of mine.
It a menu based navigation system that allows the user to do certain thigns. One of those thigns is generate some basic plots.
The menu system continues to be called inside of an infinte while loop until the user selects exit.
The problem i have is the following.
Because the program is running, after the user uses one of the menu options to make some plots that now exist in the working directory, the user can not open the plots because the program is still activley running like it should be unless the user selects exit. Its sitting there waiting for the next thing the user wants to do.
I need to find a way to let the user look at plots in the working direcotry (open them) while the program is still running.
"pause" should of been able to do this but it cant. The plots will still not open while pause is being called.
using the "keyboard" function does actually work. if i put a "keyboard" in, the program waits. The user can open plots and such.
However, the only way to return from the keyboard function is to manually type DBCONT in the terminal. This is not acceptable for a user app.
What i would like to do, is have a message that pops up and says "press any key to continue" in the background the keyboard function is called so they can open plots.
The question is. how do i programattically call DBCONT when a user pushes any key after having called the keyboard function?
I have not found a way yet or an alternative. Sorry, i am restricted from posting the code. An example can made by jsut typing the pause command in a matlab terminal and trying to open a figure. It wont let you. Then try it with keyboard and you will see you can open a figure.
but how to get back into the main loop is the question.
Thanks

Réponses (4)

Bruno Luong
Bruno Luong le 28 Fév 2023
Modifié(e) : Bruno Luong le 28 Fév 2023
Here is one way of calling dbcont to exit debug mode without user typing it this FEX
keyboard
... wait until user do his/her stuff
inputemu('key_normal','dbcont\enter'); % File exchange

Jan
Jan le 27 Fév 2023
Modifié(e) : Jan le 27 Fév 2023
What does "make some plots" exactly mean? .fig-files? PNG-files with raster copies of diagrams? What is the reason that they cannot be "opened" and with which software do you want to open them? Why does the running program prevent the "plots" to be opened?
Using keyboard to stop the execution might be working, but is sounds like a poor design. Remember that the user have an open debug console and inputs can have strange side-effects.
"how do i programattically call DBCONT when a user pushes any key after having called the keyboard function?" - My answer is the same as in the other discussion we had today: The best solution is to avoid the need to do so. In your case an explicit solution might be to let the created figures stay open (if this is meant by "make some plots"). Why are the "plots" closed before?
Another option is to avoid terminal applications with text menus, which run in a loop until the user selects "exit". This sounds like MS-DOS interfaces from 1985. Wouldn't a standard GUI be much more user-friendly?
By the way: You could use FEX: textinject and java.awt.robot to magically type "dbcont" in the command window. This is the ultimative hacking style and far beyond clean code. I'd avoid such indirections, because they let the complexity of the code explode and reduce the independency from the platform.
Summary: Refactor your code to use a standard GUI instead of fiddling in the debug console.
  7 commentaires
Robert Scott
Robert Scott le 27 Fév 2023
Modifié(e) : Robert Scott le 27 Fév 2023
We can close this, it appears there is no solution.
Jan
Jan le 28 Fév 2023
@Robert Scott: "There are millions of console applications in the world that do not require a gui." - This is correct. These applications are "single-threaded". This means, that the main task controls the program flow and does not leave a way for the user, to use the Matlab session to anything else, e.g. to open figures. This means, that a console application cannot solve your needs.
Modern software, e.g. operating systems, are multi-threaded: You can leave the current application and use another one to process other data. This can be done in a Matlab session by a GUI, which blocks the execution only while its callbacks are processed (see the 'Interruptible' property), but afterwards you have full access to Matlab's engine, while the GUI is waiting in the background for the next user interaction.
I've tried it for some days to use the command window for user interactions also, struggeled with keyboard and dbcont or tricky java hacks, which infiltrates the command window's input and output. Now I'm convinced, that there is no clean and stable way to do this. Even the suggested textinject and java.awt.robot are ugly hacks only (by the way, did you trie them?).
An experienced programmer needs less than an hour to create a GUI with some menu elements programmatically. The AppDesigner or even the outdated GUIDE can be useful also.

Connectez-vous pour commenter.


Steven Lord
Steven Lord le 27 Fév 2023
Because the program is running, after the user uses one of the menu options to make some plots that now exist in the working directory, the user can not open the plots because the program is still activley running like it should be unless the user selects exit. Its sitting there waiting for the next thing the user wants to do.
If having this application be menu-based is a hard requirement that cannot be changed, I think one option is to have a menu option that allows the user to select a file to open (either by typing the name of a figure file in the current directory or by using something like uigetfile) then have the program open the file.
  6 commentaires
Robert Scott
Robert Scott le 27 Fév 2023
Just as a side note, the keyboard command was actually the solution provided by mathworks in my support ticket.
Jan
Jan le 28 Fév 2023
Another option is to open a 2nd Matlab session to examing the figures:
!matlab -r "openfig('your.fig')"

Connectez-vous pour commenter.


Jan
Jan le 1 Mar 2023
Modifié(e) : Jan le 1 Mar 2023
This took 4 minutes for writing:
function MainGUI
FigH = figure('Name', 'Main GUI', ...
'MenuBar', 'none', ...
'NumberTitle', 'off', ...
'Resize', 'off', ...
'BusyAction', 'cancel', ... % Block new actions during computing
'DeleteFcn', @Exit_CB, ...
'CloseRequestFcn', @Exit_CB);
StartButtonH = uicontrol(FigH, 'Style', 'PushButton', ...
'String', 'Next request', ...
'FontSize', 20, ...
'Units', 'Normalized', ...
'Position', [0.1, 0.55, 0.8, 0.35], ...
'Callback', @Start_CB);
ExitButtonH = uicontrol(FigH, 'Style', 'PushButton', ...
'String', 'Exit', ...
'FontSize', 20, ...
'Units', 'Normalized', ...
'Position', [0.1, 0.1, 0.8, 0.35], ...
'Callback', @Exit_CB);
end
function Start_CB(StartButtonH, EventData)
% Disable the button temporarily:
origString = get(StartButtonH, 'String');
set(StartButtonH, 'String', 'Please wait...', 'Enable', 'off');
drawnow;
% Call your script or function containing the menu here
yourTool;
% Restore the button:
set(StartButtonH, 'String', origString, 'Enable', 'on');
end
function Exit_CB(FigH, EventData)
set(FigH, 'DeleteFcn', '');
delete(FigH);
end
Remove the loop from your code and let the GUI trigger the next call instead. When the script "yourTool" is finished, the Matlab session is free for any access, while the GUI is waiting in the background.
In a next step you can include the menu of your script and the input() command in the figure until the command window is not needed anymore. A dropdown menu or edit field might be useful and is implemented by the same uicontrol() commands.
With such a GUI you can omit the keyboard command and have no need to inject a dbcont.

Catégories

En savoir plus sur Desktop dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by