MATLAB Answers

0

One instance running issue

Asked by duong mai on 18 Apr 2019
Latest activity Commented on by Walter Roberson
on 19 Apr 2019
Hello everyone. I am designing an application, which every time that the user run excutive file, the last screen (or the current screen) will pop up, instead of opening new window.
At here, I try to use global variable current_section and my main program is return_to_section.m. Inside each gui program, this global variable is assigned to a specific value (from 0 to 3).
function return_to_section()
global current_section
if current_section==0
figure(main_screen());
return
elseif current_section==1
figure(first());
return
elseif current_section==2
figure(second());
return
elseif current_section==3
figure(third());
return
elseif isempty(current_section)==1
main_screen();
end
When I run the code in debug mode of matlab, it works well and satisfies my requirement. But when I deploy this into an application and run it, the new screen (main_screen) is opened every time I run the excutive file.
Could someone tell me why and how can I overcome this situation. Thanks you in advance. (All the files are attached below, If you want to run the app, you might need to install mcr version 9.0.1 from matlab 2016a)

  0 Comments

Sign in to comment.

Products


Release

R2016a

1 Answer

Answer by Walter Roberson
on 18 Apr 2019

Each run of the compiled executable is a separate process. The global variable will be destroyed between runs, same as if you had quit and reentered MATLAB for the interactive version. You would need to store the information about the last screen in some file (or in the registry, I suppose).
You just might be able to take advantage of setpref()... I have not explored whether that works in a compiled environment.

  2 Comments

Hi Mr Roberson,
Thanks for your comment, as your suggestion I tried using addpref/getpref, and I realize that the variable in this preference can be persisted (better than global variable). Now I can run right screen, but the issue is still remained. In matlab debug mode, everything works fine, but when I use the app (which is deployed), it opens new window, instead of pop up the current window..
function return_to_section()
%global current_section
current_section=getpref('screen_number','value');
if current_section==0
figure(main_screen());
return
elseif current_section==1
figure(first());
return
elseif current_section==2
figure(second());
return
elseif current_section==3
figure(third());
return
elseif isempty(current_section)==1
main_screen();
end
Capture.PNG
Could I ask you about where is these preference variable stored in the system?
The preferences are stored in the directory returned by prefdir(), in file matlabprefs.mat
prefdir() is a built-in, so I do not know what it chooses for executables. https://www.mathworks.com/help/matlab/ref/prefdir.html

Sign in to comment.