Thoughts on the Engineering Industry

A blog covering engineering, technology and business topics

Archive for the month “March, 2013”

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?

Four Basic Types of Loops and Conditionals in Matlab

Hello everyone.  I’ve been enjoying my spring break from school.  I’ve looked over material for a school project and plan on applying for jobs this weekend.  I hope your week is going well.  Today I wanted to describe two types of functions in Matlab, the loop and conditional, as it is used in coding for Matlab.  A loop is a function used to repeat a cycle.  A conditional is used to decide which calculation or function to perform based on a set of criteria.  Matlab has four types of loops and conditionals available by default: if conditional, while loop, for loop, and switch conditional.  In this post, I will describe to you each type, how to use them, and give a basic example.

If conditional

Description: The if conditional is a function that takes a piece of data, checks for a mathematical condition, and then uses that to apply the desired function.

Standard Usage: This function is commonly used to perform calculations that accurately account for large quantities of diverse data.

Example: In this case, y=-2+2*4=6



if x < 0


elseif x < 2






While Loop

Description: The while loop executes a function as long as a mathematical condition is true.

Standard Usage: This is commonly used to iterate a process until a specific criterion is met.

Example: In this case, y=1+2+3+4+5+6+7+8+9=45


y=0; x=0;

while x < 10





For Loop

Description: This is a loop that executes a function for a set number times.

Standard Usage: This is commonly used to perform calculations on a specific set of data for each individual data element.  This can also be used to perform calculations for cases where a specific number of iterations are used.

Example: y=[2,4,6,8]


x=1:1:4; y=zeros(4);

for i = 1:length(x)




Switch Conditional

Description: This is a conditional that takes a value or string variable and uses it choose which calculation to perform.

Standard Usage: This is commonly used to write a program to execute a variety of calculations depending on a user specified situation.

Example: In this case, y=4




switch lower(calc)

case {‘linear’}


case {‘square’}


case {‘cubic’}



disp(‘y is unknown’)



These are the four types of loops expressed in a very basic way.  This information can also be found by typing help and the name of the function, ie ‘help if’.  However, I thought this would be helpful because this shows them in their simplest form while also telling you when and how they are used.  When learning to program in Matlab, the issues I found when using loops and conditionals were not always how to find the information but figuring what function to use and how to use it in a specific circumstance.  Hopefully, this post will help with that if you are initially learning this language and get stuck with these types of functions.

If you a learning how to use loops in Matlab right now, what do you find difficult and easy?  If you already have experience using this, what did you find difficult and easy when you first learned this?  Is there still anything specific about these loops and conditionals that cause issues?  Let me know if you have any questions and have a good week!

Update on Future Blog Topics

Hello everyone.  I just wanted to update ya’ll on my plans for future topics.  In writing this last post on Matlab, I realized that there are many more topics that I can cover and decided to expand on a few of those.  Also, I’ve already talked with one guy on twitter for a bit about Matlab and he said he would appreciate my help.  If anyone else has any questions or wants help let me know – as I told him, I’m far from a professional level coder but I do know a lot about Matlab so I should be able to help some.  Next week, I plan on writing a basic description of all the different types of loops and recommended times to use them.  Another topic I have thought about discussing is the concept of creating data sets and the different ways of compiling that data (vectors, matrices, iterative, etc.).  Post any questions you have in the comments section and I’ll see if I can write a post about it.

Basic Tips for Programming in Matlab

Hello everyone.  I meant to have this ready for last weekend but I got a bit of a cold the week before and had two exams this week…what can I say, life throws us curve balls sometimes.  However, I am back in rhythm now.  Today, I want to talk about some basic tips to program well in Matlab.  Matlab is heavy duty math program that is very good at processing large matrices of data.  Calculations can be performed in the command window but the quickest way is to bring in the data and run a script file with the appropriate calculations.  An application I am doing currently is taking seismic data and processing the large blocks of test data to find the desired patterns/parameters.  I’ve taken a break from doing anything in Matlab for a while now and I’ve had to relearn a lot of these tips recently, but these should speed up your script writing process quickly.

Use comments to section and annotate parts of code – An easy to read code is essential to speeding up the process.  And not only does it speed up the writing of the program, but gives you a reference later if you have to use it after several years.  Some good things to specify in my opinion are the author, purpose, input, out, and calculation methods/processes for each section.

Use help function, lookfor function, and the simulink website to get more information – Matlab has some great references in their program and on their website.  If some help is needed in using a function or finding a function, search for it and with a bit of reading you should be able to get moving in the right direction if you know the basics of coding.

Initially clear the screen and variables and close all figures – By clearing everything out, it makes it very easy to see the results or figure out what went wrong.  I can’t tell you how many times I have caused issue from old data being in my workspace or on my command screen, especially when starting out.  Save yourself the trouble and clean everything up before running the program; put those commands at the start of the script file if possible.

Import data from excel by using the import command, then save the workspace – If you have large amounts data to pull in, there is an import command you can use in the workspace area.  This speeds up the process of bringing in data exponentially.  If it’s not needed, don’t worry too much about it; but since I am assuming that a large amount of data is being used, this issue will come up eventually.  Once you have the variables set up like you want, save the workspace.  A command can then be used to open a file of the saved variables.  This whole process saves time entering the data and minimizes lines of code on the script file.

Be as patient and methodical as possible – The only thing worse than having to fix a mistake in a script file is having to fix a mistake in a large script file.  The best thing to prevent that from happening is to take the coding process one step at a time.  Write a few lines of code, check and see you are getting the results you want, then move on to the next few lines.  It will save you a lot of headaches in the long run.

Know when to simplify or allow for more complexity – When playing around with data (especially experimental data), there are a lot of methods you can use to calculate the desired results.  These methods can range from very simple to very complex.  The skill in properly using these methods and the different commands available in Matlab is knowing when to keep it simple and when it is worth it to try something more complex.  Doing something complex and having it work inconsistently is a waste and it would probably be better to simplify it.  However, if you see a pattern and something more complex that might work, it is worth to try doing it.

These are the basic concepts I have learned in using Matlab.  There are more specific practices that are good to know as well, and once you get better you will learn those as well hopefully.  Jumping in with very little experience though, I don’t think it is good worry too much about the specifics because it would become too confusing.  Focus on these tips and pick up the rest with time and practice – not to mention I probably don’t know well enough to teach you them either.  Along with that, there are other standard things I do and with time each person will develop their own as well.  Do what you think works well and don’t stress out if someone else does or doesn’t do it until it becomes a critical issue.  Coding is a lot like writing in that everyone has a unique voice and once you embrace your style it goes a lot smoother.

I hope these tips help with you Matlab coding.  What are some basic tips that you have for coding in Matlab?  Are there some common mistakes you make that might be considered basic?  Are there any important mistakes that I missed?

Post Navigation