A01.m - Hello World: The Fancy Version
% the following 3 commands are useful housekeeping tools to help tidy-up % your workspace clc % clear command window clear % clear all variables close all % close all graphics windows % prints a message to the command window using the disp (display) command disp('hello world') % adds 1 + 1 and stores the result (2) in the variable x. Because the % statement has a semi-colon at the end, the result is not printed to the % command window x = 1+1; % this calculation has no semicolon, so the result 6 is printed to the % command window y = x + 4
We modify the basic Hello World program in a few ways:
- We add comments. Comments start with the % sign and show up in green in the Matlab editor. They are notes that you write to yourself or others who may someday try to decipher your code.
- The commands clc, clear, and close all, clear the command window, clear away any old variables that might have been previosly defined, and close out any graphical windows that might have been open. They make sure your program starts "fresh" without any unwated clutter from previous calculations.
- We show how to suppress calculations from displaying to the command window by placing a semicolon at the end of the statement. Without a semicolon, the result of the calculation is automatically printed to the command window. Suppressing the output speeds up the code especially when large arrays are used.