1 Input / Output Input – reads/gets data for the program Output – the product, after processing Both can be: interactive I/O (while program is running)

Slides:



Advertisements
Similar presentations
File Management in C. What is a File? A file is a collection of related data that a computers treats as a single unit. Computers store files to secondary.
Advertisements

Introduction To MATLAB Programming
BITS Pilani, Pilani Campus TA C252 Computer Programming - II Vikas Singh File Handling.
Programming with MATLAB
Fprintf and other examples. Save command >> !ls >> a = 3; >> save afile a >> !ls afile.mat >> !dir Directory of C:\MATLAB6p5\work\save 11/21/ :30.
MATLAB Input-Output Statements Nafees Ahmed Asstt. Professor, EE Deptt DIT, DehraDun.
EGR 106 – Truss Design Project (cont.) Truss design programs Graphical interface tools in Matlab Saving and loading data Formatted output Project Assignment.
General Computer Science for Engineers CISC 106 Lecture 04 Roger Craig Computer and Information Sciences 9/11/2009.
General Computer Science for Engineers CISC 106 Lecture 07 Dr. John Cavazos Computer and Information Sciences 2/25/2009.
ES 100: Lecture 7 String Functions 1.Log in 2.Open MATLAB 3.Change the current directory and path to U: 4.Change the numeric display to compact (File,
Input/Output Functions Selim Aksoy Bilkent University Department of Computer Engineering
Input/Output Functions Selim Aksoy Bilkent University Department of Computer Engineering
Division Example 2x - 3y + 4z = 10 x + 6y - 3z = 4 -5x + y + 2z = 3 A*X = B where A = B = >> X = A\B X =
Formatted Output What we know: write(*,*) x print *, x General Form: write (unit, format, options) list unit = * : write to the monitor format = *: no.
Introduction to MATLAB Northeastern University: College of Computer and Information Science Co-op Preparation University (CPU) 10/29/2003.
Input/Output Functions Selim Aksoy Bilkent University Department of Computer Engineering
The textread Function It is designed to read ASCII files that are formatted into columns of data Each column can be of a different type It is useful for.
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
Chapter 9 Above: An early computer input/output device on the IBM 7030 (STRETCH)
Chapter 18 I/O in C. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Standard C Library I/O commands.
Extending MATLAB Write your own scripts and/or functions Scripts and functions are plain text files with extension.m (m-files) To execute commands contained.
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
Chapter 4 MATLAB Programming Combining Loops and Logic Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files called M- files. M-files are.
AN ENGINEER’S GUIDE TO MATLAB
INTRO TO PROGRAMMING Chapter 2. M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files.
Slide deck by Dr. Greg Reese Miami University MATLAB An Introduction With Applications, 5 th Edition Dr. Amos Gilat The Ohio State University Chapter 4.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (3): MATLAB Environment (Chapter 1)
1 Functions 1 Parameter, 1 Return-Value 1. The problem 2. Recall the layout 3. Create the definition 4. "Flow" of data 5. Testing 6. Projects 1 and 2.
Programming I Introduction Introduction The only way to learn a new programming language is by writing programs in it. The first program to.
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
Matlab Training Session 10: Loading Binary Data Course Website: Training Sessions.htm.
Matlab Programming for Engineers Dr. Bashir NOURI Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
ENG College of Engineering Engineering Education Innovation Center 1 Array Accessing and Strings in MATLAB Topics Covered: 1.Array addressing. 2.
Asking the USER for values to use in a software 1 Input.
ENG College of Engineering Engineering Education Innovation Center 1 More Script Files in MATLAB Script File I/O : Chapter 4 1.Global Variables.
1 File Handling. 2 Storage seen so far All variables stored in memory Problem: the contents of memory are wiped out when the computer is powered off Example:
Chapter 3 Input and Output
GENERATION OF RANDOM NUMBERS
COMP 116: Introduction to Scientific Programming Lecture 29: File I/O.
EGR 115 Introduction to Computing for Engineers Formatted File Input / Output Wednesday 12 Nov 2014 EGR 115 Introduction to Computing for Engineers.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Files Tutor: You will need ….
Matlab Data types, input and output. Data types Char: >> a = ‘ Jim ’ Char: >> a = ‘ Jim ’ Numeric: uint8, uint16, uint32, uint64 int8, int16, int32, int64.
MATLAB for Engineers, by Holly Moore. ISBN © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
1 CSC103: Introduction to Computer and Programming Lecture No 28.
Using and Programming with MATLAB as an Engineering Tool [ Part III ]
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
Input/Output Functions. 2 MATLAB Basics: Data Files save filename var1 var2 … save homework.mat x y  binary save x.dat x – ascii  ascii load filename.
FILE I/O: Low-level 1. The Big Picture 2 Low-Level, cont. Some files are mixed format that are not readable by high- level functions such as xlsread()
Input Output IO and datafiles Credits also to Prof Selim Aksoy (Bilkent University.
Reading and Writing Data Files
Working with files Saving and loading Matlab variables to and from .mat files does not require any special file handling, just use save() and load() However,
Using Script Files and Managing Data
Programming in C Input / Output.
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
Lecture 13 Input/Output Files.
INPUT & OUTPUT scanf & printf.
Matlab Training Session 5: Importing Data
funCTIONs and Data Import/Export
Note on Indexing of Array Elements
Introduction to MATLAB
Input/Output Functions
Input/Output Functions
Presentation transcript:

1 Input / Output Input – reads/gets data for the program Output – the product, after processing Both can be: interactive I/O (while program is running) (RAM memory) or file I/O: (read/write to file) (Hard disk Memory)

2 Interactive Input Built-in functions for interactive input input menu keyboard (pause)

3 input: input(‘string’) displays the text in string on the screen, and waits for the user to give a keyboard input >> n = input('How many years? ' ) % prompts the user to input an integer number, % and saves it in n We will see on the command window: How many years?

4 input: input(‘string’) displays the text in string on the screen, and waits for the user to give a keyboard input >> n = input('How many years? ' ) % prompts the user to input an integer number, % and saves it in n We will see on the command window: How many years? 12 n = 12

5 Input (cont.): The input may also be a string >> more = input('More Simulations ? (y/n): ', 's' ) % Prompts the user to type "y” for % yes or “n” for no, and stores the % input as a string in “more”. The second argument, “ s ”, of the command directs MATLAB to save the user input as a string

6 >> more = 'y'; >> while (more == 'y') disp('eat') more = input('Are you hungry?','s') end eat Are you hungry? more = y eat Are you hungry? more = y eat Are you hungry? more = y eat Are you hungry? more = n >> y y y y Example

7 menu: menu(‘name’,’option1’,’option2’,..) creates an on-screen menu named as we requested in ‘name’, with several options The user selects any option using the mouse

8 PopOption=menu(‘Initial size','200', '250','300','350')

9 PopInitials=[ 200, 250, 300, 350]; Pop=PopInitials(PopOption) Pop= 350 PopOption=menu(‘Initial size','200', '250','300','350') PopOption = 4

10 Interactive Output We have seen some already by naming a variable, without ; it shows on output window: if we write A we get the whole matrix A= 1 2 3… if we write A(10,:) we get ans= If we don’t want to see the ans=, we use the function disp

11 disp(x) displays whatever is in the parentheses It can be anything - a string, an integer, a real number, a vector, an array x= 'A string needs to be between single quotes' ; y = 4; z=6.8; u =[ ]; >> disp(x); disp(y); disp(z); disp(u) A string needs to be between single quotes

12 plot(x,y) Plots y as a function of x Often very useful to display results Example from your program Basics: Pop =

13 >>plot(Pop(:,1),Pop(:,2))

14 File Input/Output Reading-from and writing-to a file Save Load fopen fclose fprintf fscanf frewind Writing and reading MATLAB data General text files

15 save(‘fileName’) Saves your workspace (all variables defined in the Command Window) in a file named filename.mat When called within a function, saves all the variables of defined within. load(‘fileName’) Restores the saved variables. `

16 save('fileName',varName) Saves the variable “varName” in a file named filename.mat

17 fopen: Before we use a file (for reading or writing) - must be opened with this function The function returns an integer number, which is used as a file identifier so that other functions can access this file.

18 fid = fopen ('filename.txt', 'r') opens a file called filename.txt for reading of data Assigns an id-number to it in the background for us, the file is referred to as ‘fid’ File handleFile namePermission mode Permission modes: ‘r’ – read ‘w’ – write (overwrite) ‘a’ – write (append)

19 If the opening operation fails (e.g., you ask to open for reading a non-existing file), the function returns -1 as the value behind the handle, so you know there is problem f1=fopen('input6.txt','r') f1 = 27

20 fprintf used to write formatted data into a file The structure of this function is: fprintf(fid, FORMAT string, list of variables) fid: the file identifier, i.e., the handle of the open file, on which we write

21 fprintf(fid, FORMAT string, list of variables ) FORMAT string: a string (enclosed in ‘ ‘) containing conversion specifications Each specifier in this string is headed by the character “%” There are a number or different specifiers-

22 fprintf(fid, FORMAT string, list of variables ) FORMAT string: a string (enclosed in ‘ ‘) containing conversion specifications fid = fopen ('filename.txt', 'w'); fprintf(fid,'The answer is %d not %f\n',42,1.5); Creates a new file with a single line The answer is 42 not

23 fprintf(fid,'The answer is %d not %f\n',42,1.5); Specifier (integer)Conversion specification (floating point) Special character (new line)

24 Any scalar can be written in different forms: a string, an integer, a real number, a character etc. For example, the number 67 can be written as-

25 Specifier output meaning %d67 (an integer) %10d 67 (an integer shifted 10 places to the right) %f (a real number with 6 digits after the decimal point) %.1f67.0 (a real number with 1 digit after the decimal point) %.3f ( a real number with 3 digits after the decimal point) %10.3f ( a real number with 3 digits after the decimal point, and the whole number takes 10 spaces) %cC (a character, ASCII code of 67) %e e+001 (a real number in scientific notation) Hence: fprintf(f1,'%d %5d %f %.1f %.3f %8.3f ', 67,67,67, 67, 67, 67)

26 x = 0:.2:1 fid1 = fopen('test.txt','w'); fprintf(fid1,'%6.2f \n',x); Produces on the screen x = But produces in a file named “test.txt” due to the \n : Writing vectors to file

27 >> x = 0:.2:1 x = >> y = [x ; exp(x)] y = Writing an array to a file

28 >> fprintf(fid,'%6.2f %6.3f\n',y'); Writes to the file

29 fclose A file on which we wrote must be closed to see its new contents A file from which we read must be closed and reopened (or rewind) to read again from its beginning fclose(fid) closes the file associated with the identifier “fid”. The function returns 0 if successful and -1 if not. fclose('all') % closes all open files

30 Example : %Temptable- generates and writes a temperature % table script file to generate a Fahrenheit-Celsius % temperature table. The table is written in a file % named ‘Temperature.table’ F=-40:5:100 C=(F-32)*5/9 t=[F;C]; fid=fopen('Temperature.table','w'); fprintf(fid,' Temperature Table\n'); fprintf(fid,' ________________\n'); fprintf(fid,' Fahrenheit Celsius\n'); fprintf(fid,' %4i %15.2f\n',t) fclose(fid);

31 Temperature Table ________________ Fahrenheit Celsius When we open the file “Temperature.table” we find

32 fscanf A = fscanf(fid, FORMAT, SIZE) Reads into A formatted data from file “fid” Converts it according to FORMAT string The format string is recycled through the file until an end-of-file is reached or the amount of data specified by SIZE is read in.

33 x = 0:.2:1; y = [x; exp(x)]; % Here we created it fid = fopen('exp.txt','w'); fprintf(fid,'%6.2f %6.3f\n',y); fclose(fid); fid=fopen('exp.txt','r') % open it again for reading % read from it A=fscanf(fid,'%f %f',[2,inf]) % 2 rows only, whole length % inf can be used only in the second dimension A = Example – Let’s open and read from file we recently wrote on:

34 fclose(fid); % close and open the file again fid=fopen('exp.txt','r') % Now asking just for 2 rows and 3 columns A=fscanf(fid,'%f %f',[2,3]) A =

35 frewind frewind(fid) Sets the file position indicator to the beginning of the file “fid” Useful if we need to read same file again Replaces closing and opening again