How to have a button in an GUIDE GUI close another .m script?

3 ビュー (過去 30 日間)
Joseph Kutteh
Joseph Kutteh 2021 年 5 月 24 日
編集済み: Adam Danz 2021 年 5 月 25 日
How can I program a button in a GUIDE GUI in MATLAB to close another .m script?
i am using R2019b
Thank you!

回答 (1 件)

Adam Danz
Adam Danz 2021 年 5 月 24 日
編集済み: Adam Danz 2021 年 5 月 25 日
I'm not sure how this would be useful since a program shouldn't rely on what m-files are opened.
Nevertheless, define the file name of the opened file in the first line below. If the file is opened in the editor, it should close. Otherwise, nothing should happen.
filename = 'myfile.m';
openedfiles = matlab.desktop.editor.getAll;
allfnames = cellstr({openedfiles.Filename}); % see note below
idx = endsWith(allfnames, filename);
close(openedfiles(idx))
Note: with Matlab Online (R2021a, Linux) openedfiles.Filename returns strings but on my local system (R2021a, Windows) openedfiles.Filename returns a cell array of character vectors. cellstr() is used to satisfy both systems.
  3 件のコメント
Adam Danz
Adam Danz 2021 年 5 月 25 日
I just changed the 3rd line by replacing square brackets with curly brackets. That should fix it.
Adam Danz
Adam Danz 2021 年 5 月 25 日
I'm going to change the answer one more time so that it works across multiple platforms. Oddly, with Matlab Online (R2021a, running Linux) openedfiles.Filename returns strings which is why the square brackets worked. But when I run this on my home installation (R2021a, Windows), openedfiles.Filename returns character vectors within a cell array. To satisfy both systems, I'm adding cellstr() which will convert the string arrays and will leave the cell arrays unchanged.
Duplicate question
I noticed your duplicate question on this same topic posted at about the same time as your comment above. I know it can be frustrating to wait for an answer but please have patience rather than repeating the question in a new thread. Only 35.9% of the questions asked in the past 24 hours have received an answer and yours was one of them. You were also fortunate to get a response to your follow-up comment within an hour which is pretty good for free help 😉.

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

カテゴリ

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