% Intro to Matlab programming SOIL 5110, SU 2013
%
% To introduce you to Matlab let's try some easy programming statements.
% A programming language can be seen as any spoken language, in which words,
% sentences, and punctuation symbols define the meaning of what is written.
% In coding, a statement is like a sentence in a spoken language, it
% represents the minimum meaninful command. If I say "I", or "like", or
% "pizza" each of these words separately mean nothing. However, if I say "I
% like pizza", then the sentence has a clear meaning. In coding is somewhat
% the same. If I write "num", or "=", or "5", each of these alpha
% numeric symbols separetely do not have a clear meaning, but if I write
% "num = 5" then we know that the variable called num is equal to 5.
%
% A variable is a word/identifier that hangs onto one or several values. It
% is something that we define because we know we will need to use it,
% either immmediately in the next coding step or in more advanced stages of
% the code.
%
% The symbol "%" allows for comments. So, whatever follows a percent symbol
% will be interpreted as comment, and it will be ignored by Matlab and it will not affect the code at all. 
% Comments are extremely useful to understand what a piece of code does. 
% This might not seem evident now, but when you have hundreds or thousands lines 
% of code, comments can really make the difference.
% They are also helpful to find errors, and useful when sharing codes. 
% Comments should be short and meaningful.
%
% The use of "%% " (see there are two %% and one whitespace) means that we
% are dividing a piece of code into sections or cells. Cell mode is useful
% when you want to run a code by pieces. I personally like cells to keep my
% code organized. Cell division is also useful to run tutorials like this
% so the student can go step by step.

%% Eventually, let's do some coding...
% Below you have a variable called "name" which is equal to my name.

name = 'Andres';

% as you can notice my name "Andres" is between APOSTROPHES, which means we
% are defining a string (words are always stored as strings). The semicolon at the very end of the statement
% means that we are supressing the variable output in the command
% window. Although, the variable will appear in the workspace.
% Press ctrl+enter to run this cell. Go to the workspace and see what just
% happened. Then come back for more coding, I will still be here waiting for you!

%% Now, type "Hello World" in the variable "myFirstVariable"

myFirstVariable = 'type here';

% Run the cell.
% Go to the command window and type "myFirstVariable". This will display the value stored in myFirstVariable. 
% Watch out when you type!, Matlab, as many programming languages, is case sensitive!
% There you have it, your very first step in Matlab programming!

%% Words must be stored as strings. However, numbers can also be stored as strings.
% This means that Matlab will see a number as text. 

numString = '345';


%% Now we will create a numeric variable called "num1" by writing:

num1 = 5;

% Fill in with any number the next variable called "num2". You can write an
% integer or a floating number (number with decimals such as 1.984). In this 
% case since we want to define a number, you will need to eliminate the
% apostrophes as well.

num2 = 'introduce any number here';

% We have created two numeric variables, "num1" and "num2". If you run the code using the cell
% by cell evaluation mode (ctrl+enter) you will see that these variables show up
% in the workspace.

%% The last thing we will do is using some of the variables that we created to perform 
% a mathematical operation.

addition = num1 + num2

% Observe that in this case there is not a semicolon at the end of the statement.
% This is because we want the result to appear in the command window whenever we run the cell.
% Matlab may underline part of the statement to show a possible syntax error. In
% this particular case, the code will run anyways, but Matlab suggests that
% we may need a semicolon to suppress the output. 
% Press ctrl+enter to run the cell.
%% You can also call the variable within itself. For instance:
num1 = num1 + 1;
% The code above says that "num1" is equal to the current value of "num1"
% plus one. If num1=5, after running the statement the value for num1 will
% be 6. This may not seem important at this point, but it can be very useful
% when using loops.
%% To see the variables that you have created type "whos" in the command window. 
% All variable names, as well as their dimension and class should be displayed in the command window.

%% You can figure out the number of characters in myFirstVariable by:

length(myFirstVariable)

% See we do not need to type "Hello World" because now we are using a
% variable that has "Hello World" stored in it. The use of variables makes coding much easier.

%% You can even access any character in the string by:
myFirstVariable(4)

% or

myFirstVariable (4:7)

% Observe that white space counts as a character. 

% If you want to clear one variable simply type "clear variableName". Then
% type whos (or look in the workspace) to check that the variable that you chose was deleted.
% Lastly, type "clear all" to remove all variables, and then "clc" to
% clear everything in the command window.

% Final comment. You can get to hundred lines pretty easily!! (see left of
% the screen). We've got this far because I included many comments and white spaces.
% White space is free, and keeps you code well organized.



