48

Introduction to MATLAB variables


Article posted on Thursday, 23rd, February, 2012 at 1:22 pm


The great thing about MATLAB is that it doesn’t care what type of variables you’re using. This is very good for coding up a script / function / program in no time at all. It’s not so good if you accidentally reuse a variable name, with a different type (i.e. setting var1 = ‘hello’ and then later, reusing┬ávar1 = 123) . This will end up making problems with your program that are harder to trace.

Since MATLAB doesn’t want you to decide on a variable type yourself, it comes up with the most appropriate one for you. Sometimes it is better to explicitly set these if the default types are not right for your purposes, but that’s a bit more advanced.

  • To assign a variable name to a value, you use the equals sign.
  • You only need to set a variable name and the value you want to give it.
  • This is true for any of the simple variable types:

Strings get set as:

my_string = 'hello there!';

Numbers get set as:

my_number = 862;

Note that MATLAB has many numeric types, but the default is ‘double’, which basically means it is stored as a double-precision number.

Boolean values (either true or false) get set as:

my_boolean = true;

Arrays get set as:

my_array = [];

There are a lot more types, but these are the basics that you’ll want to be using at first.



Tags: , , ,

Join in the Conversation


comments powered by Disqus