CE 436 Traffic Engineering Topic: Introduction

CE 436 Traffic Engineering Topic: Introduction

User controlled input/output Dr. Khalid F. Alkahtani GE 209 Computer Programming King Saud University Outline

Introduction USER-DEFINED INPUT OUTPUT OPTIONS Display Function Formatted OutputThe fprintf Function Formatted OutputThe sprintf Function 2 .Outline cont

READING AND WRITING DATA FROM FILES Importing Data Exporting Data DEBUGGING YOUR CODE Error Bar Debugging Toolbar 3

Introduction Written in the editing window, where the programmer and the user may be different people. The input command pauses the program and prompts the user for input; the disp and fprintf commands provide output to the command window. 4

USER-DEFINED INPUT Create more general programs by allowing the user to input values of a matrix from the keyboard while the program is running to enter a one- or two-dimensional matrix. z = input('Enter a value: ') z = input('Enter values for z in

brackets: ') 5 Data entered with input does not need to be numeric information. x = input('Enter your name in

single quotes ') An alternative form of the input command alerts the function to expect character input without the single quotes. x = input('Enter your name ', 's') 6 Exercise Create an M-fi le to calculate the area A of a triangle: Using function: function Triabgle_Area =TriAr(b,h)

Triabgle_Area=0.5.*b.*h Using controlled input/output ? 7 OUTPUT OPTIONS There are several ways to display the contents of a matrix. The simplest is to enter the name of the matrix, without a semicolon. x = 1:5; Display Function

disp(x) disp('The values in the x matrix 8 are:'); Character arrays store character information in arrays similar to numerical arrays. disp('The values in the x matrix are:'); disp(x)

9 num2str (number to string) function. disp(['The values in the x array are:' num2str(x)]) A = ['The values in the x array are: 'num2str(x)]

10 Formatted OutputThe fprintf Function fprintf(format-string, var,. . .) cars = 5; fprintf('There are %f cars in the pasture', cars) 11 12

cars = 5; fprintf('There are %f cars in the pasture \n', cars) cars = 6; fprintf('There are %f cows in the pasture \n', cars)

13 Width and precision fields voltage = 3.5; fprintf('The voltage is %8.2f millivolts \n',voltage) x = 1:5;

fprintf(%8.2f \n',x) 14 feet = 1:3; inches = feet.*12; table = [feet;inches] fprintf('%4.0f %7.2f \n',table)

use the feet and inches matrices without combining them into the table matrix fprintf(%4.0f %7.2f \n', feet, inches) 15 fprintf function can be used to send formatted output to a file. 1. file_id = fopen('my_output_file.txt', 'wt'); 2. fprintf(file_id, 'Some example output is %4.2f \n', pi*1000)

16 Formatted OutputThe sprintf Function The sprintf function is similar to fprintf , but instead of just sending the result of the formatted string to the command window, sprintf assigns it a name and sends

it to the command window. 17 a = sprintf('Some example output is %4.2f \n', pi*1000) Used as a notat Saved both number&character in one variable

18 table Function table Create output of both numbers and characters

g=[9.8;1.6] d= 0.5*g*100^2 p= { Earth; Moon} *cell array table(p,g,d) 19 table(p,g,d, 'VariableNames', {'Planet', 'g', 'Distance'}) disp(table(p,g,d, 'VariableNames', {'Planet', 'g', 'Distance'}))

20 MATLAB offers a technique for entering ordered pairs of x - and y -values graphically. The ginput command allows the user to

select points from a figure window and converts the points into the appropriate x - and y -coordinates. [x,y] = ginput (n), choose n points or [x,y] = ginput choose any point and then press enter 21 Ex. x = 5:30; y = x.^2 - 40.*x + 400; plot(x,y) axis([5,30,-50,250]) [x,y] = ginput (2)

22 READING AND WRITING DATA FROM FILES Importing Data doc fileformats The Import Wizard can be used for simple ASCII files and for Excel spreadsheet files. uiimport('ex.xlsx') Exporting Data xlsread(filename.extension') p=xlsread('ex.xlsx') xlswrite('exout.xls', p)

23

Recently Viewed Presentations

  • Presentation on Supplimentory Food Products

    Presentation on Supplimentory Food Products

    The term nutraceutical was originally defined by Dr. Stephen L. DeFelice, founder and chairman of the Foundation of Innovation Medicine (FIM), Crawford, New Jersey in 1989. Nutraceutical, a term combining the words "nutrition" and "pharmaceutical". It is a food or...
  • Test Methods - Portland Cement Association

    Test Methods - Portland Cement Association

    The side of the disk and one end is sealed. The bare end is placed in water and the uptake of water is measured as a function of time. The water gain is plotted as a function of the square...
  • Apply September 2017 - City College Norwich

    Apply September 2017 - City College Norwich

    astarfuture.co.uk . UCAS Tariff. Some course providers use UCAS Tariff points in their entry requirements too. This is a points total achieved by converting qualifications such as A levels (and many others) into points, making it simpler for course providers...
  • North Carolina Youth Accountability Planning Task Force

    North Carolina Youth Accountability Planning Task Force

    North Carolina Youth Accountability Planning Task Force Doreen Cavanaugh, Ph.D. Health Policy Institute Georgetown University April 22, 2010 ...
  • Environmental Literacy Project Michigan State University Activity 2.2:

    Environmental Literacy Project Michigan State University Activity 2.2:

    Slides 10 and 11 focus on proteins, including the atoms in protein molecules (CHON) Protein in Carrots (Plant Roots) How much protein is in 100 grams of carrots? Slides 10 and 11 focus on proteins, including the atoms in protein...
  • Country Experience :Bangladesh

    Country Experience :Bangladesh

    Application of digital map at data collection, audio-visual and small group training at the training phase and rational distribution of work, good governance in census operation and application of ICR technology in data capture and data processing will hopefully end...
  • Topic 3: Introduction to Statistics Algebra 1 Table

    Topic 3: Introduction to Statistics Algebra 1 Table

    Shape Definitions: Symmetric: if the right and left sides of the graph are approximately mirror images of each other. Skewed to the right (right-skewed) if the right side of the graph is much longer than the left side.
  • Diapositive 1 - Physique / Chimie - Lycée

    Diapositive 1 - Physique / Chimie - Lycée

    Tracer l'axe optique. Placer la lentille de centre optique O et les foyers objet F et image F' . Placer l'objet AB orienté vers le haut. 2,5 cm 2,5 cm Tracer trois rayons caractéristiques issus de B : B O...