# GETTING STARTED QUESTIONS: Getting started

FOCUS QUESTION: How do I start using MATLAB?

## EXAMPLE 1: Read data from a file into the MATLAB workspace

```   load count.dat;
```

## EXAMPLE 2: Plot the data just read

```   figure
plot(count)
```

 Questions Answers What does plot(count) do? This MATLAB command plots each column of count as an individual line graph. MATLAB plots the values against the numbers 1, 2, 3, 4, ..., 24, because count has 24 rows. Since count has three columns, you see three separate line graphs. The three graphs appear on the same figure. What does the horizontal axis on this graph represent? The horizontal axis represents the positions or row numbers within the data, starting with row 1. What happens if I omit the figure command? If a current Figure Window already exists, the plot overwrites the plot displayed in that window. Otherwise, MATLAB automatically creates a new Figure Window. Use figure each time you plot in order to see all of the figures that you make. Why don't these commands end with a semicolon? Neither the figure nor the plot command produce any output, so it isn't necessary to end them with a semicolon.

## EXAMPLE 3: Plot the data in an informative way

Type the following in the lesson 1 script, save, and run:

```   figure
plot(count)
title('Traffic at 3 San Antonio intersections')
xlabel('Hour of day (24 hour format)')
ylabel('Number of vehicles')
legend('Durango','DeZavala', 'LP410')
```

You should see a Figure Window with a labeled plot of the traffic:

 Questions Answers What is xlabel? The xlabel identifier names a MATLAB function that sets the x-axis label on the current axis. Similarly, the ylabel function sets the y-axis label on the current axis, and the title function sets a title over the current axis. Why call a function such as xlabel rather than editing the x-axis directly using the plot tools? Calling xlabel, ylabel, and title documents the purpose of the graph in your script as well as labeling the graph. If you need to make a change on the graph, you simply change the script and rerun rather than recreating a new plot manually. What is a legend? A legend is an annotation on a plot identifying the type of data represented by objects in the plot. What does the word legend represent in MATLAB? The legend identifier names a MATLAB function that provides annotations for the objects on a graph. The function arguments (the items in parentheses) specify how to identify the plot objects in the annotation. Why is 'DeZavala' enclosed in quotes? MATLAB uses single quotes to distinguish strings or labels from variable and function names. Without the quotes, DeZavala would be a variable and the value it holds would be used. When should I use a legend? If your axes have more than one graph, you should always use a legend. By calling the legend function rather than waiting to edit the plot later, you will provide documentation for your graph in your script.

## EXAMPLE 4: Reorganize your lesson1Script file into cells

 Questions Answers What is cell mode? Cell mode is a way of developing and executing MATLAB scripts in the Editor. The script is organized into subsections or cells, which you can execute individually. What is a cell? A cell is a group of statements in a MATLAB script that starts with "%% " and ends with the start of the next cell. You can execute the statements within a cell as a unit, separately from the rest of the script. Why is cell mode useful? Cell mode allows you to develop scripts in a step-by-step manner and test each step for correctness. This strategy makes it much easier to develop code that works. How do I put in cell dividers? Each time you insert a line that starts with %% followed by at least one blank, you start a cell. You can also use Cell->Insert Cell Break from the MATLAB menu bar or you can press the insert cell icon on the Editor toolbar. How do I evaluate or execute a cell? You can right click anywhere in the cell and choose Cell->Evaluate Current Cell from the MATLAB menubar. You can also use the Editor icons or to execute the current cell. What determines the current cell? The current cell is where your cursor is. (It should be highlighted in pale yellow.) How do I start a new cell? Create a line starting with two percent signs followed by a space. After placing the cursor where you want to insert the cell, you can either type these characters on the keyboard or hit the Insert cell break icon . What if I forget the space? The lines that follow will be part of the previous cell. What does the single percent sign designate? The % marks the start of a comment. Does MATLAB execute comments? No, comments are for the benefit of the user and are ignored during execution.

## EXAMPLE 5: Publish your final script.

 Questions Answers What is the Command Window? The Command Window is a place where you can directly enter and execute MATLAB commands. The Command Window also displays errors generated when you execute a script. What is a script? A script is a file containing MATLAB commands. Scripts should have the .m file extension. What is an m-file? An m-file is a special type of MATLAB file that has a .m file extension. You can store a list of MATLAB commands (a script) as an m-file. Later you will also create your own functions and save them as m-files. What is the difference between an m-file and a published script? An m-file contains commands that MATLAB can execute. A published script is a document that shows the results of running the script in a readable format. By default, the published version appears as a web page, but other forms are possible. Why bother saving commands in an m-file? These saved commands can be reused and modified, saving time and effort. Scripts allow you to automate data analysis when you have a large number of similar data sets. With scripts you can develop complicated analysis in a step-by-step manner and the script documents your steps. Can I add commands from the Command History window into an existing m-file? You can either drag commands from the Command History window into the m-file, or you can use cut and paste. Can I create an m-file from the Command History window? You can highlight commands in the Command History window, right click, and choose Create m-file. As with other applications, use Shift-click to add items to the selection and Ctrl-click to remove items from the selection.

These questions were written by Kay A. Robbins of the University of Texas at San Antonio and last updated by Dawn Roberson on 3-Jan-2014. Please contact Kay.Robbins@utsa.edu with comments or suggestions.