Tips & Tricks
Follow


Hans Scharler

Starter MATLAB Script

Hans Scharler on 29 Mar 2024
Latest activity Reply by Image Analyst on 19 Apr 2024

I am often talking to new MATLAB users. I have put together one script. If you know how this script works, why, and what each line means, you will be well on your way on your MATLAB learning journey.
% Clear existing variables and close figures
clear;
close all;
% Print to the Command Window
disp('Hello, welcome to MATLAB!');
% Create a simple vector and matrix
vector = [1, 2, 3, 4, 5];
matrix = [1, 2, 3; 4, 5, 6; 7, 8, 9];
% Display the created vector and matrix
disp('Created vector:');
disp(vector);
disp('Created matrix:');
disp(matrix);
% Perform element-wise multiplication
result = vector .* 2;
% Display the result of the operation
disp('Result of element-wise multiplication of the vector by 2:');
disp(result);
% Create plot
x = 0:0.1:2*pi; % Generate values from 0 to 2*pi
y = sin(x); % Calculate the sine of these values
% Plotting
figure; % Create a new figure window
plot(x, y); % Plot x vs. y
title('Simple Plot of sin(x)'); % Give the plot a title
xlabel('x'); % Label the x-axis
ylabel('sin(x)'); % Label the y-axis
grid on; % Turn on the grid
disp('This is the end of the script. Explore MATLAB further to learn more!');
Image Analyst
Image Analyst on 19 Apr 2024
If you want a short script that illustrates the basics, you should throw in an if statement, a for loop, and a while loop, and maybe a short call to a custom function. And of course finish up with a call to logo.
Chen Lin
Chen Lin on 1 Apr 2024
This can be a good quiz after you complete MATLAB Onramp course.
DGM
DGM on 29 Mar 2024
This brings to mind one thing that I keep thinking about. Back when I had to teach undergrad MATLAB labs as a TA, a lot of the assignments really focused on what I think now probably shouldn't have received as much attention in the shallow scope of the course material.
In particular, there was a lot of attention paid to learning how to use the colon operator for vector creation -- perhaps too much. At no point was anyone acclimated to using linspace(), so everyone came away with the skills to create vectors which were typically not going to completely span an intended interval. Even here on the forum, you see plenty of students trying to beat the three-term colon syntax into giving a uniformly-spaced vector over a specified interval. I suppose it's a quick way to learn about fencepost errors.
So yeah, I think linspace() and colon operator should have been part of the same lesson is I guess what I'm saying. Maybe that seems obvious, but it's something I wish I could go back and fix.
Jon
Jon on 3 Apr 2024
Regarding TA experience and MATLAB coding, my pet peeve is teaching students to begin every script with (ok the op's example only uses clear and close all):
clc;
clear;
close all;
Each of these commands can be useful at times, and can be typed at the command line when needed. My issue is with sticking them into the start of every script.
I generally find it useful to see my previous commands.
clearing masks problems with improperly, or uninitialized variables, as well being annoying when I actually want to have access to a previously computed variable in the workspace.
Likewise, closing figures, unintentionally is annoying when I may have wanted to compare a new plot to one that I had created earlier.
Even worse, students start to put these lines into the beginnings of function, which then causes further problems.
Hans Scharler
Hans Scharler on 29 Mar 2024
Also, I would love to hear about your TA experience sometime.
Hans Scharler
Hans Scharler on 29 Mar 2024
'linspace' is a great idea to include. Thanks for the note.
Rik
Rik on 29 Mar 2024

My personal pet peeve for this code would be clear instead of clearvars. It does the same, but it is probably worth reinforcing not using clear unless you really honest to goodness mean to use it.

See Also

Tags