<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">\documentclass[onecolumn,letterpaper,11pt]{article}

%===================================================================
% Include common packages, environments, counters etc
\input{common_latex/common_latex_packages}
\input{common_latex/common_latex_lab_format}
\input{common_latex/common_latex_definitions}
%===================================================================
\setcounter{tocdepth}{2}
%===================================================================

\begin{document}
\wikidoc{file=Lab.Matlab01}
\wikidoc{auth=mikerb@mit.edu}
\helplevelsubsec

%===================================================================
% Build the Cover Page in an environment style easy for wiki parsing
\begin{labcover}
\labtitle{Matlab Plotting Lab 01}
\vspace{0.2in}
\labcourse{Pavlab: Getting Started with Matlab Plotting}
\vspace{0.5in}
\labpng{0.25}{figures/matlab.png}
\vspace{0.25in}
\labdate{Aug 1st , 2021}
\vspace{0.23in}
\labauth{Michael Benjamin, mikerb@mit.edu}
\labauth{Department of Mechanical Engineering}
\labauth{MIT, Cambridge MA 02139}
\labtoc{small}
\labtocdepth{2}
\end{labcover}

%===================================================================
% Add a blank page so first body page is on the right when doubleside
\blankpage

%===================================================================
% Begin the body of the text
%===================================================================
\section{Overview and Objectives}
%===================================================================

\begin{labinfo}
%\labinfoheader{Topics:}
\begin{packed_itemize}[2]
\item Goal - Make a plot with manually entered data
\item Goal - Plotting data from a file
\item Goal - Running a matlab script 
\item Goal - Plot two datasets into one plot
\item Goal - Bar plots
\item Goal - Bar plots with error bars
\end{packed_itemize}
\end{labinfo}

%=======================================================================
\subsection*{Preliminaries}

Make sure that Matlab is installed on your computer. On Linux or MacOS systems
you should be able to confirm from the command line:

\vspace{0.1in}
\begin{consoleverb}
$ which matlab
/Applications/MATLAB_R2022b.app/bin/matlab
\end{consoleverb}

\vspace{0.1in}
\noindent
After launching matlab, a window like the below should open:


\vspace{0.1in}
\begin{figure}[H]
  \centering
  \includegraphics[width=0.9\textwidth]{figures/matlab_main.png}
  \caption{The initial Matlab window upon launch}
  \label{fig_matlab_main}
\end{figure}
\vspace{0.1in}

\noindent
Assuming thing look like the above, you should be good to proceed.

%=======================================================================
\section{Simple Plotting}
%=======================================================================

%=======================================================================
\subsection{A First Plot with Labels}

In the Command Window of Matlab a first simple plot can be generated with
the following commands:

\vspace{0.1in}
\begin{consoleverb}
;; wget https://oceanai.mit.edu/matlab/examples/example1A.m
&gt;&gt; x=0:1:100
&gt;&gt; y=x.^2
&gt;&gt; xlabel('x')
&gt;&gt; ylabel('x^2')
&gt;&gt; plot(x,y)
\end{consoleverb}

\vspace{0.1in}
\noindent
A separate pop-up window should appear looking something like:

\vspace{0.1in}
\begin{figure}[H]
  \centering
  \includegraphics[width=0.6\textwidth]{figures/plot1A.png}
  \caption{A simple plot of $y=x^2$}
  \label{fig_plot1A}
\end{figure}
\vspace{0.1in}

\noindent
Note the y-axis label is rotated to be rendered from bottom to the top of
the plot. This is useful for longer labels. For this short label, it can
be rotated to be rendered from left to right:

\vspace{0.1in}
\begin{consoleverb}
&gt;&gt; ylabel('x^2', 'Rotated', 0)
\end{consoleverb}

%=======================================================================
\subsection{Plotting Multiple Lines in a Single Graph}

Multiple plots may be overlayed in the same graph. Below is a simple example
that extends the first example.

\vspace{0.1in}
\begin{consoleverb}
% wget https://oceanai.mit.edu/matlab/examples/example1B.m
% open example1B.m
% &gt;&gt; example1B
&gt;&gt; x=0:1:100
&gt;&gt; y=x.^2
&gt;&gt; z=x.^1.8
&gt;&gt; xlabel('x')
&gt;&gt; ylabel('f(x)')
&gt;&gt; plot(x,y)
&gt;&gt; hold on            &lt;-- prevents the first plot from being overwritten
&gt;&gt; plot(x,z)
&gt;&gt; hold off
\end{consoleverb}

\vspace{0.1in}
\begin{figure}[H]
  \centering
  \includegraphics[width=0.6\textwidth]{figures/plot1B.png}
  \caption{A simple plot of $f(x)=x^2$ and $f(x)=x^{1.8}$}
  \label{fig_plot1B}
\end{figure}
\vspace{0.1in}

%=======================================================================
\subsection{Plotting Multiple Lines with a Legend}

\noindent
Note in Figure \ref{fig_plot1B}, the Y axis is a bit harder to express
with a single bit of text since two plots are rendered. Rather than
labeling it $x^2$, it is labeled more generically labeled $f(x)$. To
discern which line represents which plot, a legend is used:


\vspace{0.1in}
\begin{consoleverb}
% wget https://oceanai.mit.edu/matlab/examples/example1C.m
% open example1C.m
% &gt;&gt; example1C
&gt;&gt; x=0:1:100
&gt;&gt; y=x.^2
&gt;&gt; z=x.^1.8
&gt;&gt; xlabel('x')
&gt;&gt; ylabel('f(x)')
&gt;&gt; plot(x,y, 'DisplayName', 'f(x) = x^2')
&gt;&gt; hold on            
&gt;&gt; plot(x,z, 'DisplayName', 'f(x) = x^{1.8}')
&gt;&gt; legend
&gt;&gt; hold off
\end{consoleverb}

\vspace{0.1in}
\begin{figure}[H]
  \centering
  \includegraphics[width=0.65\textwidth]{figures/plot1C.png}
  \caption{A simple plot of $f(x)=x^2$ and $f(x)=x^{1.8}$ with a legend}
  \label{fig_plot1C}
\end{figure}
\vspace{0.1in}


%=======================================================================
\subsection{Controlling the Location and Font of the Graph Legend}

\noindent
Note that the legend in Figure \ref{fig_plot1C} is drawn over part of the
data itself, which is less than optimal. The location of the legend may
be controlled by passing it a \var{'Locatation'} setting. For example:

\vspace{0.1in}
\begin{consoleverb}
% wget https://oceanai.mit.edu/matlab/examples/example1D.m
% open example1D.m
% &gt;&gt; example1D
&gt;&gt; x=0:1:100
&gt;&gt; y=x.^2
&gt;&gt; z=x.^1.8
&gt;&gt; xlabel('x')
&gt;&gt; ylabel('f(x)')
&gt;&gt; plot(x,y, 'DisplayName', 'f(x) = x^2')
&gt;&gt; hold on            
&gt;&gt; plot(x,z, 'DisplayName', 'f(x) = x^{1.8}')
&gt;&gt; legend('Location','northwest', 'FontSize', 20)    % Note the difference
&gt;&gt; hold off
\end{consoleverb}

\vspace{0.1in}
\noindent
The example above should look similar to:
\vspace{0.1in}
\begin{figure}[H]
  \centering
  \includegraphics[width=0.65\textwidth]{figures/plot1D.png}
  \caption{A simple plot of $f(x)=x^2$ and $f(x)=x^{1.8}$ with a legend in
  the north-west corner with larger font size.}
  \label{fig_plot1D}
\end{figure}
\vspace{0.1in}

%=======================================================================
\subsection{Adding a Plot Title}

\noindent
A title can be added to the plot. The title may be structured to have a
sub-title. The sub-title example is given below:

\vspace{0.1in}
\begin{consoleverb}
% wget https://oceanai.mit.edu/matlab/examples/example1E.m
% open example1E.m
% &gt;&gt; example1E
x=0:1:100
y=x.^2
z=x.^1.8
[t,s] = title('A Simple Plot Example','Two plots varying the exponent of x','Color','blue');
% title('A Simpler Plot Title')
hold on
xlabel('x')
ylabel('f(x)')
plot(x,y, 'DisplayName', 'f(x) = x^2')
plot(x,z, 'DisplayName', 'f(x) = x^{1.8}')
legend('Location','northwest', 'FontSize', 12)
hold off
\end{consoleverb}

\vspace{0.1in}
\noindent
The example above should look similar to:

\vspace{0.1in}
\begin{figure}[H]
  \centering
  \includegraphics[width=0.65\textwidth]{figures/plot1E.png}
  \caption{A simple plot of $f(x)=x^2$ and $f(x)=x^{1.8}$ with a legend in
    the north-west corner with larger font size. This plot also contains
  a title and sub-title.}
  \label{fig_plot1E}
\end{figure}
\vspace{0.1in}



%=======================================================================
\section{Plotting Data from a File}
%=======================================================================

%=======================================================================
\subsection{A Simple Plot from Data Read from a File}
In most cases plotted data will be read in from a file. In a simple
example, the file \var{example1F.txt} has the following data:

\vspace{0.1in}
\begin{fileverb}
0 0
1 1
2 4 
3 9
. . .
9 81
10 100
\end{fileverb}
\vspace{0.1in}

\noindent
This data is plotted with square markers with red edge color and a custom
face color:

\vspace{0.1in}
\begin{consoleverb}
% wget https://oceanai.mit.edu/matlab/examples/example1F.txt
% wget https://oceanai.mit.edu/matlab/examples/example1F.m
% open example1F.m
% &gt;&gt; example1F
clf
A=load('example1F.txt')
plot(A(:,1),A(:,2), 'Marker', 's', 'MarkerSize', 16, 'MarkerEdgeColor', 'red', ... 
   'MarkerFaceColor', [0.7 0.4 0.4])
\end{consoleverb}


\vspace{0.1in}
\noindent
The example above should look similar to:

\vspace{0.1in}
\begin{figure}[H]
  \centering
  \includegraphics[width=0.65\textwidth]{figures/plot1F.png}
  \caption{A simple plot of $f(x)=x^2$ with data from a file.}
  \label{fig_plot1F}
\end{figure}
\vspace{0.1in}


%=======================================================================
\subsection{A Simple Plot from Two Columns of Data Read from a File}

Given a file with three columns of data, the second and third columns
can both be plotted. 

\vspace{0.1in}
\begin{fileverb}
% x  x^2  100-x^2
0    0    100
1    1     99
2    4     96
3    9     91
. . .
9    81    19
10 100  0
\end{fileverb}
\vspace{0.1in}

\noindent
Both columns of data are plotted: 

\vspace{0.1in}
\begin{consoleverb}
% wget https://oceanai.mit.edu/matlab/examples/example1G.m
% open example1G.m
% &gt;&gt; example1G
clf
A=load('example1F.txt')
plot(A(:,1),A(:,2), 'Marker', 's', 'MarkerSize', 16, 'MarkerEdgeColor', 'red', ... 
   'MarkerFaceColor', [0.7 0.4 0.4])
hold on
plot(A(:,1),A(:,3), 'Marker', 's', 'MarkerSize', 12, 'MarkerEdgeColor', 'black')
\end{consoleverb}


\vspace{0.1in}
\noindent
The example above should look similar to:

\vspace{0.1in}
\begin{figure}[H]
  \centering
  \includegraphics[width=0.65\textwidth]{figures/plot1G.png}
  \caption{A simple plot of $f(x)=x^2$ and $f(x)=1-x^2$ with data from a file.}
  \label{fig_plot1G}
\end{figure}
\vspace{0.1in}


%=======================================================================
\subsection{Plotting Data from a File Named as Matlab Argument}

In this next example, we return to plotting a single line of data from
a file. But in this case the file is named in an argument to a Matlab
function.

\vspace{0.1in}
\begin{fileverb}
% File 1: data1.txt
% x  x^2 
0     0   
1     1   
2     4   
3     9   
. . .
9    81  
10  100 
\end{fileverb}
\vspace{0.1in}

\vspace{0.1in}
\begin{fileverb}
% File 1: data2.txt
% x   100-x^2
0     100
1      99
2      96
3      91
. . .
9      19
10      0
\end{fileverb}
\vspace{0.1in}

\noindent
It is our first example of a Matlab function (versus a script). 

\vspace{0.1in}
\begin{consoleverb}
% wget https://oceanai.mit.edu/matlab/examples/example1H.m
% open example1H.m
% &gt;&gt; example1H data1.txt
function example1H(arg1)
clf
A=load(arg1)
plot(A(:,1),A(:,2), 'Marker', 's', 'MarkerSize', 16, 'MarkerEdgeColor', 'red', ... 
   'MarkerFaceColor', [0.7 0.4 0.4])
end
\end{consoleverb}

\vspace{0.1in}
\noindent
The example above should look similar to:

\vspace{0.1in}
\begin{figure}[H]
  \centering
  \includegraphics[width=0.75\textwidth]{figures/plots1H.png}
  \caption{Two simple plots made from the same Matlab plotting function. }
  \label{fig_plots1H}
\end{figure}
\vspace{0.1in}


%=======================================================================
\subsection{Plotting Data from a File Named as Linux Shell Argument}

In our final example, we extend the previous example by showing how we can
invoke Matlab from the Linux/MacOS command line, passing in arguments
naming both the data file to be plotted, and the image file to be created.
In this example, interactive Matlab will not be launched. The goal is to
be able to invoke examples like this from within a bash script at the
culmination of an automated experiment. We're looking for a line as
simple as:

\vspace{0.1in}
\begin{consoleverb}
$ matlab plotfile.m data.txt image.jpg
\end{consoleverb}

\noindent
The below example has three additions over the previous example:

\begin{packed_itemize}
\item The main function \var{example1J} now accepts two arguments. The first
  argument is the data file to be plotted, and the second argument names the
  image file to be created from the plotted data.
\item It contains a line of the form \var{saveas(gcf,file.png)} which
  will save the plot image to a file, named in the function argument
  \var{arg2}.
\item The final addition is to add the \var{exit} line immediately
  after the plot file has been saved. This automatic exit is needed to
  bring down Matlab automatically and return to the shell command
  line.
\end{packed_itemize}

\vspace{0.1in}
\begin{fileverb}
% wget https://oceanai.mit.edu/matlab/examples/example1J.m
% matlab example1J.m -r "example1J(\"test_data1.txt\", \"test_image1.png\")"
function example1J(arg1,arg2)
clf
A=load(arg1)
plot(A(:,1),A(:,2), 'Marker', 's', 'MarkerSize', 16, 'MarkerEdgeColor', 'red', ... 
   'MarkerFaceColor', [0.7 0.4 0.4])
saveas(gcf,arg2)
exit
end
\end{fileverb}

\vspace{0.1in}
\noindent
To invoke this on the command line, run:

\vspace{0.1in}
\begin{consoleverb}
$ matlab example1J.m -r "example1J(\"data1.txt\", \"image1.png\")"
\end{consoleverb}

\vspace{0.1in}
\noindent
This invokes matlab, reading in the function defined in \var{example1J.m}.
The \var{-r cmd} component will invoke the \var{example1J} function with
two arguments surrounded by parentheses. The double-quotes around the whole
part is to shield the parenthesis from the command-line shell. 

\vspace{0.1in}
\noindent
The above example code should produce plots exactly as in Figure \ref{fig_plots1H},
the only difference being that they are created with the following two
system shell command line invocations:

\vspace{0.1in}
\begin{consoleverb}
$ matlab example1J.m -r "example1J(\"data1.txt\", \"image1.png\")"
$ matlab example1J.m -r "example1J(\"data2.txt\", \"image2.png\")"
\end{consoleverb}

\vspace{0.1in}
\noindent
In the final part of this example, we would like to disable the launch of the Matlab
interactive GUI and splash window normally displayed upon launch. Two more command
line args are added:

\vspace{0.1in}
\begin{consoleverb}
$ matlab -nodisplay -nosplash example1J.m -r \
  "example1J(\"data1.txt\", \"image1.png\")"
\end{consoleverb}

\vspace{0.1in}
\noindent
Notes:

\begin{packed_itemize}
\item The arguments \var{-nodisplay} and \var{-nosplash} come {\em before}
  \var{example1J.m}.
\item The argument \var{-r} comes {\em after} \var{example1J.m}.
\end{packed_itemize}

% what happens when/if the function doesn't match the filename?


\end{document}

</pre></body></html>