Saving a sequence of Matlab commands to a file
In this lesson, I'll elucidate on how to save a sequence of Matlab commands to a file, because that's the kind of thing that can really make your life easier.
Recording the commands you use in your calculations can help you verify your procedure, and can be especially useful when trying to identify errors in your calculations. It's a bit like keeping a log of what you've done
Now, to begin recording your commands, all you have to do is enter the command diary on in the Matlab prompt, like so:
>> diary on
This command will automatically start saving your commands to the "diary" file in the Matlab working folder. It's as simple as that.
For example, if you enter a few commands on the Matlab prompt like this:
>> a = 1
a = 1
>> b=2
b = 2
>> c=a+b
c = 3
Then, when you're finished, simply type diary off to end the recording of commands, like so:
>> diary off
And there you have it! The diary file will be saved in the Matlab working folder as a text file, containing all the commands you entered during your session.
Note. When you start recording commands multiple times in a session using "diary on" and "diary off", Matlab continues to write to the diary file without deleting the previous sequences of commands that you had already recorded.
Is it possible to change the name of the diary file?
Yes, Matlab allows you to save the sequence of commands using a file with a different name than "diary".
If you want to customize the name of the text file, you can use the "diary" command followed by the name of the file you want to use, like this:
>> diary filename
The operation is always the same.
In this case, however, Matlab writes the commands of the session in the text file that you indicated and not in the "diary" file.
To end the recording of commands, use the diary off command.
>> diary off
And that's all there is to it! With this method, you can save multiple recordings of commands using different file names.