Open an edit window where we can write the command lines. Click on the white sheet in the menu under File or simply write edit in the command window. Alternatively File->New ->M-file. The following is written in the edit window:
% M-file created by MatlabTutorials.com t=0:0.01:10; % t goes from 0 to 10 in steps of 0.01. y1=sin(t); % everything to the left of the equality sign is an % assignment, y1 is % variable. plot(t,y1), grid % plots y1 versus t and puts a grid on the plot. hold % holds the figure window and allows several plots to be made in the % same figure. y2=sin(2*t); % variable y2 is introduced and calculated. plot(t,y2) % plots y2 versus t in the same figure window as above. title('Exercise matlabtutorials.com') % puts a title in our plot. xlabel('time') % gives a label to the x-axis. ylabel('y') legend('sin(t)','sin(2*t)') % puts a box with text for different plots.
Everything that follows the %-character on the same line is regarded a comment and ignored. Also note the semicolon. It prevents output from a command to be printed out in the command window. If the command is a calculation, it is still executed. Save the m-file as exercise.m in directory work. Run the m-file by writing exercise in the command window followed by