Command Window

In the Command Window, write a for loop that will print the elements from a vector variable in sentence format.

From: Matlab (Second Edition) , 2012

Data analysis with MatLab

William Menke , Joshua Menke , in Environmental Data Analysis with Matlab (Second Edition), 2016

1.4 Navigating folders

The MatLab command window supports a number of commands that enable you to navigate from folder to folder, list the contents of folders, and so on. For example, when you type

pwd

(for "print working directory") in the Command Window, MatLab responds by displaying the name of the current folder. Initially, this is almost invariably the wrong folder, so you will need to cd (for "change directory") to the folder where you want to be—the ch01 folder in this case. The pathname will, of course, depend on where you copied the eda folder, but will end in eda/ch01. On our computer, typing

cd c:/menke/docs/eda/ch01

does the trick. If you have spaces in your pathname, just surround it with single quotes:

cd 'c:/menke/my docs/eda/ch01'

You can check if you are in the right folder by typing pwd again. Once in the ch01 folder, typing

eda01_01

will run the eda01_01 m-script, which displays the current date. You can move to the folder above the current one by typing

cd ..

and to one below it by giving just the folder name. For example, if you are in the eda folder you can move to the ch01 folder by typing

cd ch01

Finally, the command dir (for "directory"), lists the files and subfolders in the current directory.

dir

(MatLab eda01_02)

Read full chapter

URL:

https://www.sciencedirect.com/science/article/pii/B978012804488900001X

Introduction to MATLAB Programming

Stormy Attaway , in Matlab (Second Edition), 2012

2.2 Matlab scripts

Once a problem has been analyzed, and the algorithm for its solution has been written and refined, the problem is then written in a particular programming language. A computer program is a sequence of instructions, in a given language, that accomplishes a task. To execute , or run , a program is to have the computer actually follow these instructions sequentially.

High-level languages have English-like commands and functions, such as "print this" or "if x < 5, do something." The computer, however, can only interpret commands written in its machine language . Programs that are written in high-level languages must therefore be translated into machine language before the computer can actually execute the sequence of instructions in the program. A program that does this translation from a high-level language to an executable file is called a compiler . The original program is called the source code , and the resulting executable program is called the object code .

By contrast, an interpreter goes through the code line-by-line, executing each command as it goes. MATLAB uses what are called either script files, or M-files (the reason for this is that the extension on the file name is .m). These script files are interpreted, rather than compiled. Therefore, the correct terminology is that these are scripts, not programs. However, the terms are somewhat loosely used by many people, and the documentation in MATLAB itself refers to scripts as programs. In this book, we will reserve the use of the word "program" to mean a set of scripts and functions, as described briefly in Section 2.7 and then in more detail in Chapter 6.

A script is a sequence of MATLAB instructions that is stored in an M-file and saved. The contents of a script can be displayed in the Command Window using the type command. The script can be executed, or run, by simply entering the name of the file (without the .m extension).

Before creating a script, make sure the Current Folder (called "Current Directory" in earlier versions) is set to the folder in which you want to save your files.

To create a script, click on File, then New, then Script. (In earlier versions of MATLAB, click on File, then New, then M-file.) A new window will appear called the Editor. To create a new script, simply type the sequence of statements (note that line numbers will appear on the left).

When finished, save the file using File and then Save. Make sure that the extension of .m is on the file name (this should be the default). The rules for file names are the same as for variables (they must start with a letter, after that there can be letters, digits, or the underscore). For example, we will now create a script called script1.m that calculates the area of a circle. It assigns a value for the radius, and then calculates the area based on that radius.

In this text, scripts will be displayed in a box with the name of the M-file on top.

script1.m

radius = 5

area = pi * (radiusˆ2)

In the Command Window, the contents of the script can be displayed and executed. The type command shows the contents of the file named script1.m; note that the .m is not included.

>> type script1

radius = 5

area = pi * (radiusˆ2)

There are two ways to view a script once it has been written: either open the Editor Window to view it, or use the type command as shown here to display it in the Command Window.

To actually run or execute the script, the name of the file is entered at the prompt (again, without the .m). When executed, the results of the two assignment statements are displayed, since the output was not suppressed for either statement.

>> script1

radius =

  5

area =

  78.5398

Once the script has been executed, you may find that you want to make changes to it (especially if there are errors!). To edit an existing file, there are several methods to open it. The easiest are:

Click on File, then Open, and then click on the name of the file.

Within the Current Folder Window, double-click on the name of the file in the list of files.

2.2.1 Documentation

It is very important that all scripts be documented well, so that people can understand what the script does and how it accomplishes its task. One way of documenting a script is to put comments in it. In MATLAB, a comment is anything from a % to the end of that particular line. Comments are completely ignored when the script is executed. To put in a comment, simply type the % symbol at the beginning of a line, or select the comment lines and then click on Text and then Comment and the Editor will put in the % symbols for the comments.

For example, the previous script to calculate the area of a circle could be modified to have comments:

script1b.m

% This script calculates the area of a circle

% First the radius is assigned

radius = 5

% The area is calculated based on the radius

area = pi * (radiusˆ2)

The first comment at the beginning of the script describes what the script does. Then, throughout the script, comments describe different parts of the script (not usually a comment for every line, however!). Comments don't affect what a script does, so the output from this script would be the same as for the previous version.

The help command in MATLAB works with scripts as well as with built-in functions. The first block of comments (defined as contiguous lines at the beginning) will be displayed. For example, for script1b:

>> help script1b

  This script calculates the area of a circle

The reason that a blank line was inserted in the script between the first two comments is that otherwise both would have been interpreted as one contiguous comment, and both lines would have been displayed with help. The very first comment line is called the "H1 line"; it is what the function lookfor searches through.

Practice 2.1

Write a script to calculate the area of a rectangle. Be sure to comment the script.

Read full chapter

URL:

https://www.sciencedirect.com/science/article/pii/B9780123850812000028

Introduction to MATLAB

Stormy Attaway , in Matlab (Second Edition), 2012

1.2 The MATLAB Desktop Environment

In addition to the Command Window, there are several other windows that can be opened and may be opened by default. What is described here is the default layout for these windows in Version R2011a, although there are other possible configurations. Different versions of MATLAB may show other configurations by default, and the layout can always be customized. Therefore, the main features will be briefly described here.

Directly above the Command Window, there is a pull-down menu for the Current Folder. The folder that is set as the Current Folder is where files will be saved.

To the right of the Command Window is the Workspace Window on top and the Command History Window on the bottom. The Command History Window shows commands that have been entered, not just in the current session (in the current Command Window), but previously as well. (The Workspace Window will be described in the next section.) To the left of the Command Window is the Current Folder Window. This shows the files that are stored in the Current Folder. These can be grouped by type, and sorted by name. If a file is selected, information about that file is shown on the bottom.

This default configuration can be altered by clicking on Desktop, or using the icons at the top right corner of each window. These include an "x" that will close a particular window, and a curled arrow that in its initial state pointing to the upper right allows one to undock that window. Once undocked, clicking on the curled arrow pointing to the lower right will dock the window again.

Read full chapter

URL:

https://www.sciencedirect.com/science/article/pii/B9780123850812000016

Introduction to MATLAB

Stormy Attaway , in MATLAB (Fifth Edition), 2019

1.2 The MATLAB Desktop Environment

In addition to the Command Window, there are several other windows that can be opened and may be opened by default. What is described here is the default layout for these windows in Version R2018a, although there are other possible configurations. Different versions of MATLAB may show other configurations by default, and the layout can always be customized. Therefore, the main features will be described briefly here.

To the left of the Command Window is the Current Folder Window. The folder that is set as the Current Folder is where files will be saved. This window shows the files that are stored in the Current Folder. These can be grouped in many ways, for example by type, and sorted, for example by name. If a file is selected, information about that file is shown on the bottom where it says "Details."

To the right of the Command Window are the Workspace Window on top and the Command History Window on the bottom. The Command History Window shows commands that have been entered, not just in the current session (in the current Command Window), but previously as well. The Workspace Window will be described in the next section.

This default configuration can be altered by clicking the down arrow at the top right corner of each window. This will show a menu of options (different for each window), including, for example, closing that particular window and undocking that window. Once undocked, bringing up the menu and then clicking on the curled arrow pointing to the lower right will dock the window again. To make any of these windows the active window, click the mouse in it. By default, the active window is the Command Window.

The Desktop has a toolstrip . By default, three tabs are shown ("HOME", "PLOTS", and "APPS"), although another, "SHORTCUTS", can be added.

Under the "HOME" tab, there are many useful features, which are divided into functional sections "FILE", "VARIABLE", "CODE", "ENVIRONMENT", and "RESOURCES" (these labels can be seen on the very bottom of the grey toolstrip area). For example, under "ENVIRONMENT", hitting the down arrow under Layout allows for customization of the windows within the Desktop Environment. Other toolstrip features will be introduced in later chapters when the relevant material is explained.

Read full chapter

URL:

https://www.sciencedirect.com/science/article/pii/B9780128154793000015

Sights and Sounds

Stormy Attaway , in Matlab (Fourth Edition), 2017

13.3.2.1 Intro to App Designer

Typing the command appdesigner in the Command Window will bring up the App Designer. Fig. 13.29 shows the App Designer; in the middle is the blank layout under "Design View." The Component Library on the left shows the icons of the components that can be created. On the right are the Component Browser and Component Properties Windows. In the Component Browser, the default name for the blank figure, app.UIFigure, can be seen.

Figure 13.29. App Designer Layout.

>> appdesigner

Fig. 13.30 shows the remainder of the Component Library, including the Container and Instrumentation components.

Figure 13.30. Bottom of Component Library.

Clicking on "Code View" instead of "Design View" shows the code that has been created for the blank app. App Designer creates a class named App1 that is derived from a MATLAB apps superclass called matlab.apps.AppBase. The properties will consist of all of the components; for now, there is just one property, which is the UI Figure Window. There are also two private methods blocks and one public methods block.

classdef App1 < matlab.apps.AppBase

    % Properties that correspond to app components

    properties (Access = public)

  UIFigure matlab.ui.Figure % UI Figure

    end

    methods (Access = private)

  % Code that executes after component creation

  function startupFcn(app)

  end

    end

    % App initialization and construction

    methods (Access = private)

  % Create UIFigure and components

  function createComponents(app)

  % Create UIFigure

  app.UIFigure = uifigure;

  app.UIFigure.Position = [100 100 640 480];

  app.UIFigure.Name = 'UI Figure';

  setAutoResize(app, app.UIFigure, true)

  end

    end

    methods (Access = public)

  % Construct app

  function app = App1()

  % Create and configure components

  createComponents(app)

  % Register the app with App Designer

  registerApp(app, app.UIFigure)

  % Execute the startup function

  runStartupFcn(app, @startupFcn)

  if nargout == 0

    clear app

  end

  end

  % Code that executes before app deletion

  function delete(app)

  % Delete UIFigure when app is deleted

  delete(app.UIFigure)

  end

    end

end

The methods are used for all of the app functions, including eventually callback functions. For now, with just a blank UI Figure Window, the public methods include the constructor function App1, and a function delete that deletes the figure when the app is deleted. The constructor calls a private function createComponents, which creates a UI Figure using the uifigure function. It then uses the dot notation to set properties for the app.UIFigure, including its Name and Position. Note that the dot notation is used twice here, as in app.UIFigure.Name.

The function startupFcn executes when components are created. It is called by a function in the constructor. Within the Code View, most of the window is grey, which means that the code cannot be modified. However, within the startupFcn function there is a white box, which means that code can be inserted in that function. Fig. 13.31 shows the startupFcn function in Code View.

Figure 13.31. Editable portion of default app code.

We have seen the code that is generated with just a blank canvas. In the Design View of the App Designer environment, components can be dragged from the Component Library onto the blank layout. When this is done, the code will be updated to create a new property for each component, and to initialize some of its properties (for example, the Position property will be based on the location to which the component was dragged). Properties can then be modified in the Component Properties Window to the right of the layout.

For example, dragging a Label (generically named 'Label'!) into the design area causes the Design View to look like Fig. 13.32.

Figure 13.32. Simple Label in app.

Clicking on the label itself in the Design View will change "Component Properties" to "Label Properties." Properties such as the text on the label, its justification, font name, font size, and so forth can then be modified from the Label Properties window. Each of these will modify the code that is created.

Assuming that no properties have been modified, and there is just a generic label in the layout as shown in Fig. 13.32, most of the code remains the same as with just a blank UI Figure Window. Choosing Code View will show that the class App1 now has a new property Label, and the createComponents method creates a Label using the uilabel function and gives it a Position. The added code is shown in bold here (not in App Designer itself).

    classdef App1 < matlab.apps.AppBase

    % Properties that correspond to app components

    properties (Access = public)

  UIFigure matlab.ui.Figure % UI Figure

  Label   matlab.ui.control.Label % Label

    end

    % Create UIFigure and components

  function createComponents(app)

  %   Create UIFigure

  app.UIFigure = uifigure;

  app.UIFigure.Position = [100 100 640 480];

  app.UIFigure.Name = 'UI Figure';

  setAutoResize(app, app.UIFigure, true)

  % Create Label

  app.Label = uilabel(app.UIFigure);

  app.Label.Position = [421 431 30 15];

  end

    end

Modifying a property, such as changing the text of the label in the Label Properties window to be "Hello" instead of "Label" will modify the code in createComponents to:

  %   Create Label

  app.Label = uilabel(app.UIFigure);

  app.Label.Position = [421 431 30 15];

  app.Label.Text = 'Hello';

It is also possible programmatically to modify properties such as app.Label.Text in the startupFcn function, but it is preferable and easier to do this from the Design View.

To run the app, click on the green Run arrow. This brings up a dialog box that asks for the name of the file that will be created; the default name that shows is "App1.mlapp." Changing the name to "HelloLabel.mlapp" will create a file with this name and bring up a UI Figure Window. The upper right portion of this is shown in Fig. 13.33.

Figure 13.33. Text of label in UI Figure Window.

Note that the App Designer creates one file with the extension '.mlapp,' unlike GUIDE, which creates two files: a .m file and a .fig file.

While this example shows the basics of App Designer and the object-based code that it creates, it did not involve any callbacks.

Read full chapter

URL:

https://www.sciencedirect.com/science/article/pii/B9780128045251000131

Descriptive statistics

Kandethody M. Ramachandran , Chris P. Tsokos , in Mathematical Statistics with Applications in R (Third Edition), 2021

1.8.3 SPSS examples

For SPSS, we will give only Windows commands. For all the pull-down menus, the sequence will be separated by the > symbol.

Example 1.8.6

Redo Example 1.8.1 with SPSS.

Solution

After entering the data in C1:

Analyze   > Descriptive Statistics   >   Explore … >

At the Explore window select the variable and move to Dependent List ; then click Plots, select Stem-and-Leaf , click Continue , and click OK at the Explore Window.

We will get the output with a few other things, including box plots along with the stem-and-leaf display, which we will not show here.

Example 1.8.7

Redo Example 1.8.2 with SPSS.

Solution

After entering the data:

Graphs   >   Histogram … >

At the Histogram window select the variable and move to Variable , and click OK .

We will get the histogram, which we will not display here.

Example 1.8.8

Redo Example 1.8.3 with SPSS.

Solution

Enter the data, then:

Analyze  > Descriptive Statistics  > Frequencies … >

At the Frequencies window select the variable(s); then open the Statistics window and check whichever boxes you desire under Percentile , Dispersion, Central Tendency , and Distribution   >   continue   >   OK .

For example, if you select Mean, Median, Mode, Standard Deviation, and Variance, we will get the following output and more:

Statistics
VAR00001
N Valid 25
Missing 0
Mean 83.2800
Median 34.0000
Mode 14.00
Std. Deviation 128.36488
Variance 16,477.54333

Read full chapter

URL:

https://www.sciencedirect.com/science/article/pii/B9780128178157000014

Introduction to MATLAB®

Lisa A. Oberbroeckling , in Programming Mathematics Using MATLAB®, 2021

1.4 Diaries and script files

You can record your commands and output to the command window with the

Image 1

command. The commands you enter in the command window and any output are stored as an ASCII (plain text) file. The command

Image 1

toggles the recording on and off. If you do not specify a filename, it will create a file in the current folder of the name "diary." The command

Image 94

will save the recording to a file of the name "filename." The commands

Image 95

and

Image 96

will pause and restart the recording, respectively, to the active file. NOTE: when you use the

Image 94

more than once (within the same current folder), it will continue to APPEND to the file.

For example, the commands above will generate the following text in the file "filename":

1+1

ans =

2

diary off

3^2

ans =

9

diary

Script files, or m-files, are extremely useful for running and rerunning code. You may be required to turn in script files for your assignments.

Script files are ASCII files (plain text files) with extension .m; thus they are also called m-files. These are basically batch files.

When you run a SCRIPT file, MATLAB executes each line as if you typed each line into the command window.

Script files are very useful; you can edit them, save them, execute them many times and "tweak" them to experiment with commands.

The MATLAB editor window is the best way to create and edit script files.

Image 98

Comments within MATLAB files begin with the percent symbol (

Image 99
).

Running script files:

There are many ways to run an m-file of name filename.m. First, you must MAKE SURE CURRENT FOLDER IS CORRECT!

1.
Image 100
2.
Image 101
3.
Image 102
4.

Within the Editor tab, chose run...

Read full chapter

URL:

https://www.sciencedirect.com/science/article/pii/B9780128177990000065

An Introduction to Matlab®

G.R. Lindfield , J.E.T. Penny , in Numerical Methods (Third Edition), 2012

1.1.

(a)

Start up Matlab. In the command window type x = -1:0.1:1 and then execute each of the following statements by typing them in and pressing return:

sqrt(x) cos(x)
sin(x) 2./x
x.\ 3 plot(x, sin(x.^3))

plot(x, cos(x.^4))

Examine the effects of each statement carefully.

(b)

Execute the following and explain the results:

x = [2 3 4 5]

y = -1:1:2

x.^y

x.*y

x./y

Read full chapter

URL:

https://www.sciencedirect.com/science/article/pii/B9780123869425000011

Notes

William Menke , Joshua Menke , in Environmental Data Analysis with MatLab, 2012

Publisher Summary

MatLab variables accumulate in itsWorkspace and can be accessed not only by the script that created them but also through both the Command Window and theWorkspace Window. Datasets often contain time expressed in calendar (year, month, day) and clock (hour, minute, second) format. This format is not suitable for data analysis and needs to be converted into a format in which time is represented by a single, uniformly increasing variable, say t. The choice of units of the time variable, t, and the definition of the start time, t ¼ 0, will depend on the needs of the data analysis. MatLab's load() function can read only text files containing a table of numerical values. Some publicly accessible databases, including many sponsored by government agencies, provide data as more complicated text files that are a mixture of numeric and alphabetic values. MatLab provides a way to define functions that perform in exactly the same manner as built-in functions such as sin() and cos(). In MatLab, a key feature of a matrix is that its elements can be accessed with a single index, instead of the normal two indices.

Read full chapter

URL:

https://www.sciencedirect.com/science/article/pii/B978012391886400012X