MATLAB Guide

Chapter 13: Input and Output

In this chapter we discuss how to obtain input from the user, how to display information on the screen, and how to read and write text files. Note that textual output can be captured into a file (perhaps for subsequent printing) using the diary command, as described on p. 29. How to print and save figures is discussed in Section 8.4.

13.1 User Input

User input can be obtained with the input function, which displays a prompt and waits for a user response:

<span class="unicode">?</span> x = input('Starting point: ')Starting point: 0.5x =    0.5000

Here, the user has responded by typing "0.5", which is assigned to x. The input is interpreted as a string when an argument s is appended:

<span class="unicode">?</span> mytitle = input('Title for plot: ','s')Title for plot: Experiment 2mytitle =Experiment 2The function ginput collects data via mouse clicks. The command[x,y] = ginput(n)

returns in the vectors x and y the coordinates of the next n mouse clicks from the current figure window. Input can be terminated before the nth mouse click by pressing the return key. One use of ginput is to find the approximate location of points on a graph. For example, with Figure 8.7 in the current figure window, you might type [x,y] = ginput(1) and click on one of the places where the curves intersect. As another example, the first two lines of the Bezier curve example on p. 85 can be replaced by

UNLIMITED FREE
ACCESS
TO THE WORLD'S BEST IDEAS

SUBMIT
Already a GlobalSpec user? Log in.

This is embarrasing...

An error occurred while processing the form. Please try again in a few minutes.

Customize Your GlobalSpec Experience

Category: Pressure Sensors
Finish!
Privacy Policy

This is embarrasing...

An error occurred while processing the form. Please try again in a few minutes.