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

3 visualizzazioni (ultimi 30 giorni)
How can I program a button in a GUIDE GUI in MATLAB to close another .m script?
i am using R2019b
Thank you!

Risposte (1)

Adam Danz
Adam Danz il 24 Mag 2021
Modificato: Adam Danz il 25 Mag 2021
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 Commenti
Adam Danz
Adam Danz il 25 Mag 2021
I just changed the 3rd line by replacing square brackets with curly brackets. That should fix it.
Adam Danz
Adam Danz il 25 Mag 2021
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 😉.

Accedi per commentare.

Categorie

Scopri di più su Migrate GUIDE Apps in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by