One instance running issue

5 次查看(过去 30 天)
duong mai
duong mai 2019-4-18
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)

回答(1 个)

Walter Roberson
Walter Roberson 2019-4-18
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 个评论
duong mai
duong mai 2019-4-19
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?
Walter Roberson
Walter Roberson 2019-4-19
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 MATLAB Compiler 的更多信息

产品


版本

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by