How to have a button in an GUIDE GUI close another .m script?
조회 수: 3 (최근 30일)
이전 댓글 표시
How can I program a button in a GUIDE GUI in MATLAB to close another .m script?
i am using R2019b
Thank you!
댓글 수: 0
답변 (1개)
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
2021년 5월 25일
I just changed the 3rd line by replacing square brackets with curly brackets. That should fix it.
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 Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!