フィルターのクリア

GUIDE openingFnc called more than once

2 ビュー (過去 30 日間)
Yoni Sher
Yoni Sher 2018 年 2 月 4 日
コメント済み: Isaac Wolf 2018 年 3 月 30 日
Hi, Is there somewhere I can put GUIDE initialization code that is guarantied to be called exactly once? I've found that openingFnc is called every time the GUI gets focus, and I'm not happy with the "set a flag to indicate you've done this before" hack.
  2 件のコメント
Guillaume
Guillaume 2018 年 2 月 4 日
編集済み: Guillaume 2018 年 2 月 4 日
I'm not happy with the "set a flag to indicate you've done this before"
Why not? That seems the easiest and simplest way to do what you want you want and is guaranteed to work.
I certainly wouldn't call it a hack.
Yoni Sher
Yoni Sher 2018 年 2 月 5 日
Mostly because the flag doesn't exist on the first call, so it's "if ~(exist('flag') == 1)" which is not elegant.

サインインしてコメントする。

採用された回答

Jan
Jan 2018 年 2 月 5 日
Using a flag is perfect, but if ~(exist('flag') == 1) would be a very poor implementation. Remember that this would search in the complete Matlab path for the files 'flag.m', 'flag.mexw64', variables, Java classes, folders etc. also. This is slow and might not reflect what you need.
But adding a flag to the figure's ApplicationData is fine and secure:
flagIsSet = isappdata(FigureHandle, 'MyFlag');
setappdata(FigureHandle, 'MyFlag', true);
if flagIsSet
% run your code, which should run once only
end
  1 件のコメント
Yoni Sher
Yoni Sher 2018 年 2 月 5 日
Thanks, this is helpful. I was indeed trying to avoid 'exist' because I knew it was inefficient, but I didn't remember why.

サインインしてコメントする。

その他の回答 (2 件)

Image Analyst
Image Analyst 2018 年 2 月 4 日
The OpeningFcn is not called more than once for me. I've never noticed that and I've written hundreds of GUIs. I have a line at the end of my OpeningFcn that does:
% Print informational message so you can look in the command window and see the order of program flow.
fprintf('Now leaving myGuiName_OpeningFcn.\n');
I ran it. Cleared the command window, then went back to the GUI to put it in focus, and the line did not show up in the command window. So I can't reproduced. I could debug it if you attached the .m file and .fig file.
  4 件のコメント
Yoni Sher
Yoni Sher 2018 年 2 月 5 日
I used a breakpoint, but even before that I knew it was being called multiple times because if the hardware is already initialized and someone tries to start it up again, all * breaks loose.
Isaac Wolf
Isaac Wolf 2018 年 3 月 30 日
I had been having the same issue, and I found out it was due to an "ishandle()" call I had in one of my other functions to make sure the GUI was still open.

サインインしてコメントする。


Walter Roberson
Walter Roberson 2018 年 2 月 5 日
The CreateFcn callback for the GUI will only be called once when the figure is loaded from the .fig file. Be warned that this will occur before the handles structure is created. The order of CreateFcn for the various components is not explicitly defined in the documentation.

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by