Different Types of Matlab Files and How to Use Them
Hello everyone, I hope y’all are doing well. Today I wanted to talk about all the different types of files available in Matlab and the most efficient way of using them. There a two standard program files that can be used in Matlab: the script file and the function file. Along with that, there is a variable file which can be used to save the variables in the workspace. And finally, to save the results there are figure files for any figures that have been created and text files for any results that have been shown in the command window.
Script file: (.m) This is a program file to run a unique calculation for any data or process. My preference is to individually create these for each new problem or set of calculations I need to perform in Matlab. This doesn’t mean I always have to start from scratch; and if I am is lucky enough, there is minimal changing required if it’s a common issue/calculation. The best way to think about this in my mind is that this is the unique framework of the problem and anything repetitive, common, or extraneous can be performed using one of the following methods.
Function file: (.m) This is a specific type of program file which takes some given inputs, runs some standard calculations/processes, and provides the necessary outputs. All the calculations are considered separate from the script program itself; as a result, the variables and data internally used aren’t saved in the workspace unless specified as an output. This is a great tool to run a repetitive calculation or clean up a script program if it is running very slowly. These can be user defined or standard Matlab functions.
Variable file: (.mat) This file is basically a saved set of variables for the workspace. These can be loaded fairly quickly with one or two command lines at most and are great for introducing the initial data in a clean and efficient manner. I’m assuming there is also a way to save a variable file in the program as well which may be beneficial if a method for saving the data for the workspace in the future is desired.
Figure file: (.fig) This file is used to save any figures that have been created. This is a very simple concept in that its sole purpose is to save figures so that the program does not need to be run to create them every time. A great time saver for a long running script or function file.
Text file: (.txt) This is similar to a figure file. To use this efficiently, create a diary using the diary command and save it as a text file. This creates a file that shows everything done in the command window while running the script file. It pretty much serves the same purpose for the command window as the figure file does for any figures.
Synopsis: This is the combined usage of the files in most of my matlab programs. I write a script file to accomplish the unique calculations/process necessary. I set up a text file using the diary function so that all the calculations in the command window are saved. I use a variable file to bring in the initial data. I use and/or create function files to perform any extraneous or repetitive calculations if necessary. I could use a variable file to save the resulting data in the workspace, but I haven’t had to do that yet. I save any figure files that have been created. If possible, I do all of this with commands in the script file itself so that all I have to do is click run to get the results and files.
That is how I personally use those files and I find it to be the most efficient so far. Like I have mentioned before, there are different methods and every programmer has their own voice so to speak. As long as everything is properly commented and nothing too crazy is done, everything should work out fine. I just wanted to let you know my process in case any of it might help you. Have a good week!
Question of the Week:
How do you use the different Matlab files and why? If it is different from mine, do you think is better or worse than my method and why?