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

  • Prezentácia programu PowerPoint

    Prezentácia programu PowerPoint

    dávkovanie vyberať s ohľadom na možné riziko AMR, konkrétne u liekov, kde sú dávky príliš nízke. indikácie obmedziť len na tie, kde je účinnosť preukázaná, vyhnúť sa všeobecným indikáciám bez pevného klinického základu.
  • www.tvdsb.ca

    www.tvdsb.ca

    Adaptive reasoning involves students facing challenging work to build persistence and increase flexibility. Adaptive reasoning "refers to the capacity to think logically about the relationships among concepts and situations." (p.129) (Adding it Up: Helping Children Learn Mathematics, 2001)
  • Vještina Pisanja Optužnice

    Vještina Pisanja Optužnice

    Krivično djelo učinjeno sa eventualnim umišljajem (dolus eventualis) izvršilac je svjestan da može nastupiti posljedica pa je na nju pristao. Dakle izvršilac predviđa nastupanje štetne posljedice kao moguće i pristaje na tu posljedicu. Primjer: Da je Dana 15.1.2006. godine oko...
  • Civil Rights, Affirmative Action and Policy & Procedure

    Civil Rights, Affirmative Action and Policy & Procedure

    Civil Rights & Affirmative Action. All Employees of OCES have a responsibility to civil rights! CED is ultimately responsible for compliance. Everyone must abide not only by the letter of the law(s), but also the spirit of the law(s)
  • The Change, In Theory

    The Change, In Theory

    How Did Teaching Introductory Statistics Get To Be So Complicated?!? Roxy Peck Cal Poly, San Luis Obispo [email protected]
  • Commissioning the LHC Synchrotron-Light Monitors

    Commissioning the LHC Synchrotron-Light Monitors

    BSRT: Imaging telescope, for transverse beam profiles. BSRA: Abort-gap monitor, to verify that the gap is empty. When the kicker fires, particles in the gap get a partial kick and might cause a quench. Two particle types: Protons and lead...
  • Newsweek Report Card - Forsyth County Schools

    Newsweek Report Card - Forsyth County Schools

    Diploma CORE. Robinson/Fahey/Thompson. Talk about Whole Student aspect of the IB Diploma Program, how it goes beyond just book learning and teaches how to learn, how subjects connect together and the development of the learner profile.
  • Assembly Language Programming - Hiram College

    Assembly Language Programming - Hiram College

    Arial MS Pゴシック Blank Presentation Assembly Language Programming Instruction Set Design MIPS MIPS Assembler Why 3 addresses? Recall: CPSC 171 Assembler 0-address (Stack Machine) Another MIPS example Registers Why Limit Number of Registers? Registers in MIPS Assembler Revised example Try...