How to create a "macro" button on Matlab editor

The short version of my request is:
Is there a way to have a macro that combines two commands, 1. fclose all 2. exit debug mode(shift+F5)
and make it available on the editor as a button, just like there are buttons for "exit debug mode (shift+F5)", "Continue (F5)" etc.
Here is the long version:
I use Matlab to digest a datafile and display certain outputs as images. I open the datafile (txt) outside a for loop, read a bunch of lines from it at every iteration, and display certain computed outputs in a image. I close the file (using fclose) only after the for loop is over.
Most of the time, I only run a few iterations of the for loop and then press exit (Shift+F5) to stop the run. But this does not execute the "fclose(fid)" command at the end of the file. Hence, the original output file is still being "used" by Matlab as far as other programs are concerned. In order to release the file, every time I have to type "fclose all" in the command window, which is annoying. I am wondering whether I can combine "fclose all, followed by exit (shift+F5)" commands and create a macro that I access on my editor window.

Answers (1)

To solve the issue of closing the file, have you considered using an onCleanup() ?

1 Comment

I have tried this with a strange error. Here are two files I need to open in Matlab and close on exit:
fid = fopen(datFilename);
fidTrue = fopen(truthParsefile);
ctruth = onCleanup(@()fclose(fidTrue));
c = onCleanup(@()fclose(fid));
string = fgetl(fidTrue);
If I use the onCleanup commands as mentioned above, execution of the last line gives out this error:
Invalid file identifier. Use fopen to generate a valid file identifier.
However, the file identifier is correct, since I can run the code without any problem if I comment out the onCleanup command lines. Is there anything special I need to do with OnCleanup command?

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Asked:

on 2 Oct 2012

Community Treasure Hunt

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

Start Hunting!