Singular Value Decomposition (SVD) 1

Size: px
Start display at page:

Download "Singular Value Decomposition (SVD) 1"

Transcription

1 Singular Value Decomposition (SVD) 1 Let A m n be a matrix. Then σ is called one singular value associated with the singular vectors u R m 1 and v R n 1 for A provided that { Av = σu, A T u = σv. We further have { AV = UΣ, A T U = V Σ, where U and V are both unitary, and the diagonal terms in Σ are σ s, 0 s in off-diagonal terms. You may use the built-in function svd. 1 See Zheng-Liang Lu 398 / 433

2 Example: Low-rank Approximation for Image Compression This idea originates from Principal Component Analysis (PCA). 2 Use svd to calculate the principal components of the input image. Then we can have an image extremely similar to the origin one, but with a smaller image size by keeping the vectors associated with a few first largest of principal components. 2 See Zheng-Liang Lu 399 / 433

3 1 >> Lecture 6 2 >> 3 >> -- User-Controlled Input and Output 4 >> Zheng-Liang Lu 400 / 433

4 American Standard Code for Information Interchange (ASCII) 4 Everything in the computer is encoded in binary. ASCII codes represent text in computers, communications equipment, and other devices that use text. ASCII is a character-encoding scheme originally based on the English alphabet that encodes 128 specified characters into the 7-bit binary integers (see the next page). Unicode 3 became a standard for the modern systems from Unicode includes ASCII. 3 See Unicode 8.0 Character Code Charts. 4 The first version was in See ASCII. Zheng-Liang Lu 401 / 433

5 Zheng-Liang Lu 402 / 433

6 Import Tool One can load data from the specified file by importdata(filename), which can recognize the common file extensions. For example, txt, csv, jpg, bmp, wav, avi, and xls. 5 Note that the file name must be a string. If not, importdata interprets the file as a delimited ASCII file as default. importdata( -pastespecial ) loads data from the system clipboard rather than from a file. Try uiimport. 5 See supported-file-formats.html. Zheng-Liang Lu 403 / 433

7 Example 1 >> A = importdata('ngc6543a.jpg'); 2 >> image(a); % show image Zheng-Liang Lu 404 / 433

8 Example Use a text editor to create a space-delimited ASCII file with column headers like this: 1 Day1 Day2 Day3 Day4 Day5 Day6 Day Zheng-Liang Lu 405 / 433

9 1 clear; clc; 2 3 A = importdata('myfile01.txt', ' ', 1); 4 for k = [3, 5] 5 disp(a.colheaders{1, k}); % headers of columns 6 disp(a.data(:, k)); % numeric data 7 end Zheng-Liang Lu 406 / 433

10 Access to Delimited Text Files dlmread(filename, delimiter) reads ASCII-delimited file of numeric data. dlmwrite(filename, M, delimiter) writes the array M to the file using the specified delimiter to separate array elements. The default delimiter is the comma (,). dlmwrite(filename, M, -append ) appends the data to the end of the existing file. dlmread(filename, delimiter, R, C) reads data whose upper left corner is at row R and column C in the file. R and C start from 0. (R, C)=(0, 0) specifies the first element in the file. Zheng-Liang Lu 407 / 433

11 Example Write to the file. 1 >> M = gallery('integerdata', 100, [5 8], 0); 2 >> dlmwrite('myfile.txt', M, 'delimiter', '\t'); Read from the file. 1 >> dlmread('myfile.txt', '\t') 2 >> dlmread('myfile.txt', '\t', 2, 3) Zheng-Liang Lu 408 / 433

12 textread textread is useful for reading text files with known formats. [A, B, C,... ]=textread(filename, format, N) reads data from the file filename into the variables (A, B, C,... ) using the specified format, format, for N lines. format determines the number and types of return arguments. (See next page.) Without N, textread reads until the end of file. The common conversions are as follows: %d: signed integer values. %f: floating-point values. %s: strings. Zheng-Liang Lu 409 / 433

13 Example Create a file with the following lines: 1 Sally Level Yes 2 Arthur Level No 1 >> [names, types, x, y, answer] =... textread('mydata.dat', '%s %s %f %d %s') %... normal usage 1 >> [names, types, x, answer] =... textread('mydata.dat', '%s Level%d %f %*d %s',... 1) % check the difference! In %*f, * ignores the matching characters specified by *. Zheng-Liang Lu 410 / 433

14 Access to Excel Files xlsread(filename, sheet, xlrange) reads from the specified sheet and range. The variable sheet can be the sheet name 6 or the sheet number in the excel file. The variable xlrange is optional for the rectangular portion of the worksheet to read. For example, xlrange = B:B is used to import column B. To read a single value, use xlrange = B1:B1. 7 xlswrite(filename, A, sheet, xlrange) writes the array A to the specified range of the sheet. 6 The default sheet name is 工作表 1. 7 Contribution by Mr. Tsung-Yu Hsieh (MAT24409) on August 27, Zheng-Liang Lu 411 / 433

15 Example 1 >> values = {1, 2, 3; 4, 5, 'x'; 7, 8, 9}; 2 >> headers = {'First', 'Second', 'Third'}; 3 >> xlswrite('myexample.xlsx', [headers; values]);... % write 1 >> subseta = xlsread('myexample.xlsx', 1, 'B2 :... C3') % read 2 3 subseta = NaN Zheng-Liang Lu 412 / 433

16 Low-Level File I/O Low-level file I/O functions allow the most control over reading/writing data to a file. However, these functions require that you specify more detailed information about the files than the easier-to-use high-level functions, such as importdata. If the high-level functions cannot import your data, you may consider to use low-level file I/O. The normal procedure is as follow: 1. Open a file. 2. Read/write data into the file. 3. Close the file. Zheng-Liang Lu 413 / 433

17 Open Files fopen(filename, permission) create a file object in the memory: filename: the file name permission: file access type, specified as a string return an integer at least 3 as the file identifier 8 You can create a new file, make permission w. Be aware that you can repeatedly open a new file with the same file name. Note that fid is 1 if fopen fails to open the file with r. 8 Matlab reserves 1 and 2 for standard output and standard error, respectively. Zheng-Liang Lu 414 / 433

18 Low-Level File I/O Low-level file I/O functions allow the most control over reading/writing data to a file. However, these functions require that you specify more detailed information about the files than the easier-to-use high-level functions, such as importdata. If the high-level functions cannot import your data, you may consider to use low-level file I/O. The normal procedure is as follow: 1. Open a file. 2. Read/write data into the file. 3. Close the file. Zheng-Liang Lu 415 / 433

19 Open Files fopen(filename, permission) create a file object in the memory: filename: the file name permission: file access type, specified as a string return an integer at least 3 as the file identifier 9 You can create a new file, make permission w. Be aware that you can repeatedly open a new file with the same file name. Note that fid is 1 if fopen fails to open the file with r. 9 Matlab reserves 1 and 2 for standard output and standard error, respectively. Zheng-Liang Lu 416 / 433

20 Permission Codes See Zheng-Liang Lu 417 / 433

21 Example feof(fid), which refers to end-of-file, returns 1 if a previous operation set the end-of-file indicator for the specified file. fgetl(fid) returns the next line of the specified file, removing the newline characters. If the line contains only the end-of-file marker, then the return value is 1. 1 clear; clc; 2 3 f = fopen('fgetl.m', 'r'); 4 while ~feof(f) 5 disp(fgetl(f)); 6 end 7 fclose(f); Zheng-Liang Lu 418 / 433

22 Close Files fclose(fid) closes an opened file. fclose( all ) closes all opened files. fclose returns a status of 0 when the close operation is successful. Otherwise, it returns 1. Zheng-Liang Lu 419 / 433

23 fprintf Recall that fprintf(format, A,..., A n ) formats data and displays the results on the screen 11. fprintf(fid, format, A,..., A n ) applies format to all elements of arrays A 1,..., A n in column order, and writes the data to a text file. format: to specify a format for the output fields; it is a string. A 1,..., A n : arrays for the output fields. Matlab reserves the file identifier number 1 and 2 for standard output on the screen, and standard error, respectively. fprintf(1, This is standard output!\n ); fprintf(2, This is standard error!\n ); sprintf(format, A 1,..., A n ) returns the results as a string. 11 Recall disp. Zheng-Liang Lu 420 / 433

24 Example fprintf can print multiple numeric values and literal text to the screen. 1 >> A = [ ; ]; 2 >> format = 'X is %4.2f meters or %8.3f mm.\n'; 3 >> fprintf(format, A); % print on the screen 4 5 X is 9.90 meters or mm. 6 X is 8.80 meters or mm. 7 X is 7.70 meters or mm. %4.2f specifies that the first value in each line of output is a floating-point number with a field width of four digits, including two digits after the decimal point. Can you explain %8.3f? Zheng-Liang Lu 421 / 433

25 Escape Characters %%: percent sign \\: backslash \b: backspace \n: new line \t: horizontal tab Zheng-Liang Lu 422 / 433

26 Format Conversion Integer, signed: %d Integer, unsigned %u: base 10 %o: base 8 %x: base 16 Floating-point number %f: fixed-point notation %e: scientific notation, such as e+00 Characters %c: single character %s: string Note that the % can be followed by an optional field width to handle fixed width fields. Zheng-Liang Lu 423 / 433

27 Example: grep in UNIX/Linux findstr(s 1, S 2 ) returns the starting indices of any occurrences of the shorter of the two strings in the longer function grep(f, pattern) 2 f = fopen(f, 'r'); 3 cnt = 0; 4 while ~feof(f) 5 t = fgetl(f); 6 cnt = cnt + 1; 7 w = findstr(t, pattern); 8 if ~isempty(w) 9 fprintf('%d: %s\n', cnt, t); 10 end 11 end 12 fclose(f); 13 end 12 See Knuth Morris Pratt string searching algorithm (1974). Zheng-Liang Lu 424 / 433

28 Exercise Write a program which write a multiplication table into a text file Zheng-Liang Lu 425 / 433

29 1 clear; clc; 2 3 f = fopen('multiplicationtable.txt', 'w'); 4 for i = 1 : 9 5 for j = 1 : 9 6 fprintf(f, '%3d', i * j); 7 end 8 fprintf(f, '\n'); 9 end 10 fclose(f); Zheng-Liang Lu 426 / 433

30 fscanf fscanf(fid, format, i) reads data from the file specified by file identifier fid, converts it according to the string specified by format. fscanf can be used to skip specific characters in a sample file, and return only numeric data. Zheng-Liang Lu 427 / 433

31 Example 1 clear; clc; 2 str = { 3 ['78' char(176) 'C']; % char(176) is the... symbol of degree 4 ['72' char(176) 'C']; 5 ['64' char(176) 'C']; 6 ['66' char(176) 'C']; 7 ['49' char(176) 'C']}; 8 fid = fopen('data.txt', 'w'); 9 for i = 1 : length(str) 10 fprintf(fid, '%s\n', str{i}); 11 end 12 fclose(fid); 13 % main 14 fid = fopen('data.txt', 'r'); 15 A = fscanf(fid, ['%d' char(176) 'C']) 16 fclose(fid); Zheng-Liang Lu 428 / 433

32 Binary Files fread(fid, size, precision) interprets values in the file according to the form and size described by precision. fwrite(fid, A, precision) translates the values of A according to the form and size described by precision. Options for size are: N: read N elements into a column vector. inf : read to the end of the file. [M, N]: read elements to fill an M-by-N matrix, in column order. Zheng-Liang Lu 429 / 433

33 Options for precision are: uchar : unsigned integer, 8 bits. int64 : integer, 64 bits. uint64 : unsigned integer, 64 bits. float64 : floating point, 64 bits. Note that the number can be replaced by 8, 16, and 32. Zheng-Liang Lu 430 / 433

34 Example 1 clear; clc; 2 A = magic(3) 3 fid = fopen('magic3.txt', 'w'); 4 fwrite(fid, A, 'int32'); 5 fclose(fid); 6 fid = fopen('magic3.txt', 'r'); 7 fread(fid, [3 3], 'int32'); % try [3 1]? You may try different combinations, say, int64 in fwrite and int32 in fread. Zheng-Liang Lu 431 / 433

35 Access to Internet 13 urlread(url, name, value) returns the contents of a URL. 1 A =... urlread(' 2 f = fopen('matlab.html', 'w'); 3 fprintf(fid, '%s', A); 4 fclose(f); 5 dos('start matlab.html'); Try sendmail, ftp. 13 See Zheng-Liang Lu 432 / 433

36 Example: Yahoo Finance API Current market and historical data from the Yahoo! data server Blog: 研究雅虎股票 API (Yahoo finance stock API) Google: yahoo-finance-managed Historical Stock Data downloader by Josiah Renfree (2008) Zheng-Liang Lu 433 / 433

1 >> Lecture 6 2 >> 3 >> -- User-Controlled Input and Output 4 >> Zheng-Liang Lu 367 / 400

1 >> Lecture 6 2 >> 3 >> -- User-Controlled Input and Output 4 >> Zheng-Liang Lu 367 / 400 1 >> Lecture 6 2 >> 3 >> -- User-Controlled Input and Output 4 >> Zheng-Liang Lu 367 / 400 American Standard Code for Information Interchange (ASCII) 2 Everything in the computer is encoded in binary.

More information

Access to Delimited Text Files

Access to Delimited Text Files Access to Delimited Text Files dlmread(filename, delimiter) reads ASCII-delimited file of numeric data. dlmwrite(filename, M, delimiter) writes the array M to the file using the specified delimiter to

More information

Low-Level File I/O. 1. Open a file. 2. Read or write data into the file. 3. Close the file. Zheng-Liang Lu 1 / 14

Low-Level File I/O. 1. Open a file. 2. Read or write data into the file. 3. Close the file. Zheng-Liang Lu 1 / 14 Low-Level File I/O Low-level file I/O functions allow the most control over reading or writing data to a file. However, these functions require that you specify more detailed information about your file

More information

CSE 123. Lecture 9. Formatted. Input/Output Operations

CSE 123. Lecture 9. Formatted. Input/Output Operations CSE 123 Lecture 9 Formatted Input/Output Operations fpintf function writes formatted data in a user specified format to a file fid fprintf Function Format effects only the display of variables not their

More information

ES 117. Formatted Input/Output Operations

ES 117. Formatted Input/Output Operations ES 117 Formatted Input/Output Operations fpintf function writes formatted data in a user specified format to a file fid fprintf Function Format effects only the display of variables not their values through

More information

Fö 6: Filhantering. DN1212 Numeriska metoder och grundläggande programmering. Pawel Herman. KTH/CB

Fö 6: Filhantering. DN1212 Numeriska metoder och grundläggande programmering. Pawel Herman. KTH/CB Fö 6: Filhantering DN1212 Numeriska metoder och grundläggande programmering paherman@kth.se KTH/CB 22 februari Pawel 2013Herman Lecture outline Recapitulation of what we already know about I/O Matlab format

More information

Using files. Computer Programming for Engineers (2014 Spring)

Using files. Computer Programming for Engineers (2014 Spring) Computer Programming for Engineers (2014 Spring) Using files Hyoungshick Kim Department of Computer Science and Engineering College of Information and Communication Engineering Sungkyunkwan University

More information

Chapter 11 Input/Output (I/O) Functions

Chapter 11 Input/Output (I/O) Functions EGR115 Introduction to Computing for Engineers Input/Output (I/O) Functions from: S.J. Chapman, MATLAB Programming for Engineers, 5 th Ed. 2016 Cengage Learning Topics Introduction: MATLAB I/O 11.1 The

More information

PROGRAMMING WITH MATLAB WEEK 13

PROGRAMMING WITH MATLAB WEEK 13 PROGRAMMING WITH MATLAB WEEK 13 FILE INPUT AND OUTPUT Input statements read in values from the default or standard input device. In most systems the default input device is the keyboard. The input expression

More information

5. MATLAB I/O 1. Beyond the Mouse GEOS 436/636 Jeff Freymueller, Sep 26, The Uncomfortable Truths Well, hop://xkcd.com/568 (April 13, 2009)

5. MATLAB I/O 1. Beyond the Mouse GEOS 436/636 Jeff Freymueller, Sep 26, The Uncomfortable Truths Well, hop://xkcd.com/568 (April 13, 2009) 5. MATLAB I/O 1 Beyond the Mouse GEOS 436/636 Jeff Freymueller, Sep 26, 2017 The Uncomfortable Truths Well, hop://xkcd.com/568 (April 13, 2009) Topics Loading and Saving the Workspace File Access Plo$ng

More information

Working with Excel Files. a = round(rand(5,3)*100) xlswrite('rand.xlsx',a) b = xlsread('rand.xlsx')

Working with Excel Files. a = round(rand(5,3)*100) xlswrite('rand.xlsx',a) b = xlsread('rand.xlsx') File I/O Working with Excel Files a = round(rand(5,3)*100) xlswrite('rand.xlsx',a) b = xlsread('rand.xlsx') Excel files with mixed content >> [nums, txt, raw] = xlsread('texttest.xls') nums = 123 333 432

More information

Lecture 7. MATLAB and Numerical Analysis (4)

Lecture 7. MATLAB and Numerical Analysis (4) Lecture 7 MATLAB and Numerical Analysis (4) Topics for the last 2 weeks (Based on your feedback) PDEs How to email results (after FFT Analysis (1D/2D) Advanced Read/Write Solve more problems Plotting3Dscatter

More information

12-Reading Data text: Chapter ECEGR 101 Engineering Problem Solving with Matlab Professor Henry Louie

12-Reading Data text: Chapter ECEGR 101 Engineering Problem Solving with Matlab Professor Henry Louie 12-Reading Data text: Chapter 4.4-4.5 ECEGR 101 Engineering Problem Solving with Matlab Professor Henry Louie Overview Reading Data from.txt Reading Data from.xls and.csv Reading Data from.mat User Interface

More information

Common Commands in Low-Level File I/O

Common Commands in Low-Level File I/O Common Commands in Low-Level File I/O feof(fid), which refers to end-of-file, returns 1 if a previous operation set the end-of-file indicator for the specified file. tline = fgetl(fid) returns the next

More information

Beyond the Mouse A Short Course on Programming

Beyond the Mouse A Short Course on Programming 1 / 14 Beyond the Mouse A Short Course on Programming 5. Matlab IO: Getting data in and out of Matlab Ronni Grapenthin and Glenn Thompson Geophysical Institute, University of Alaska Fairbanks October 10,

More information

Lecture 5: Strings, Printing, and File I/O

Lecture 5: Strings, Printing, and File I/O 1 Lecture 5: Strings, Printing, and File I/O 1 Learning objectives At the of this class you should be able to... be able to name the three Matlab data types most commonly used in ME 352 be able to use

More information

ENG Introduction to Engineering

ENG Introduction to Engineering GoBack ENG 100 - Introduction to Engineering Lecture # 9 Files, Sounds, Images and Movies Koç University ENG 100 - Slide #1 File Handling MATLAB has two general ways of importing/exporting data from the

More information

MATLAB User-defined functions, Data Input/Output. Edited by Péter Vass

MATLAB User-defined functions, Data Input/Output. Edited by Péter Vass MATLAB User-defined functions, Data Input/Output Edited by Péter Vass User-defined functions Although, MATLAB provides a wide range of built-in functions it may often be necessary to create and use an

More information

Importing and Exporting Data

Importing and Exporting Data Class14 Importing and Exporting Data MATLAB is often used for analyzing data that was recorded in experiments or generated by other computer programs. Likewise, data that is produced by MATLAB sometimes

More information

Matlab Programming Arrays and Scripts 1 2

Matlab Programming Arrays and Scripts 1 2 Matlab Programming Arrays and Scripts 1 2 Mili I. Shah September 10, 2009 1 Matlab, An Introduction with Applications, 2 nd ed. by Amos Gilat 2 Matlab Guide, 2 nd ed. by D. J. Higham and N. J. Higham Matrix

More information

Files and Streams Opening and Closing a File Reading/Writing Text Reading/Writing Raw Data Random Access Files. C File Processing CS 2060

Files and Streams Opening and Closing a File Reading/Writing Text Reading/Writing Raw Data Random Access Files. C File Processing CS 2060 CS 2060 Files and Streams Files are used for long-term storage of data (on a hard drive rather than in memory). Files and Streams Files are used for long-term storage of data (on a hard drive rather than

More information

13-Writing Data text: Chapter ECEGR 101 Engineering Problem Solving with Matlab Professor Henry Louie

13-Writing Data text: Chapter ECEGR 101 Engineering Problem Solving with Matlab Professor Henry Louie 13-Writing Data text: Chapter 4.4-4.5 ECEGR 101 Engineering Problem Solving with Matlab Professor Henry Louie Overview Writing Data to.txt Writing Data to.xls and.csv Writing Data to.mat Dr. Henry Louie

More information

MATLAB. Data and File Management

MATLAB. Data and File Management MATLAB Data and File Management File Details Storage of data in variables and arrays is temporary. For permanent retention of data files are used. All of us are familiar with files. We save our work (e.g.

More information

Scalars and Variables

Scalars and Variables Chapter 2 Scalars and Variables In this chapter, we will discuss arithmetic operations with scalars (numbers, really an array with only one element) and variables. Scalars can be used directly in calculations

More information

Corso di Identificazione dei Modelli e Analisi dei Dati

Corso di Identificazione dei Modelli e Analisi dei Dati Università degli Studi di Pavia Dipartimento di Ingegneria Industriale e dell Informazione Corso di Identificazione dei Modelli e Analisi dei Dati Data Import Prof. Giuseppe De Nicolao, Federica Acerbi,

More information

CMPT 102 Introduction to Scientific Computer Programming. Input and Output. Your first program

CMPT 102 Introduction to Scientific Computer Programming. Input and Output. Your first program CMPT 102 Introduction to Scientific Computer Programming Input and Output Janice Regan, CMPT 102, Sept. 2006 0 Your first program /* My first C program */ /* make the computer print the string Hello world

More information

MATLAB Introduction to MATLAB Programming

MATLAB Introduction to MATLAB Programming MATLAB Introduction to MATLAB Programming MATLAB Scripts So far we have typed all the commands in the Command Window which were executed when we hit Enter. Although every MATLAB command can be executed

More information

Matlab. Carlo Tomasi. Since images are matrices of numbers, many vision algorithms are naturally implemented

Matlab. Carlo Tomasi. Since images are matrices of numbers, many vision algorithms are naturally implemented Matlab Carlo Tomasi MATLAB is a simple and useful high-level language for matrix manipulation. It is often convenient to use MATLAB even for programs for which this language is not the ideal choice in

More information

Python Working with files. May 4, 2017

Python Working with files. May 4, 2017 Python Working with files May 4, 2017 So far, everything we have done in Python was using in-memory operations. After closing the Python interpreter or after the script was done, all our input and output

More information

Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 18 I/O in C Standard C Library I/O commands are not included as part of the C language. Instead, they are part of the Standard C Library. A collection of functions and macros that must be implemented

More information

Princeton University. Computer Science 217: Introduction to Programming Systems. Data Types in C

Princeton University. Computer Science 217: Introduction to Programming Systems. Data Types in C Princeton University Computer Science 217: Introduction to Programming Systems Data Types in C 1 Goals of C Designers wanted C to: Support system programming Be low-level Be easy for people to handle But

More information

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017* 600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general

More information

Unit 4. Input/Output Functions

Unit 4. Input/Output Functions Unit 4 Input/Output Functions Introduction to Input/Output Input refers to accepting data while output refers to presenting data. Normally the data is accepted from keyboard and is outputted onto the screen.

More information

Programming in Mathematics. Mili I. Shah

Programming in Mathematics. Mili I. Shah Programming in Mathematics Mili I. Shah Starting Matlab Go to http://www.loyola.edu/moresoftware/ and login with your Loyola name and password... Matlab has eight main windows: Command Window Figure Window

More information

Standard C Library Functions

Standard C Library Functions Demo lecture slides Although I will not usually give slides for demo lectures, the first two demo lectures involve practice with things which you should really know from G51PRG Since I covered much of

More information

Computers Programming Course 5. Iulian Năstac

Computers Programming Course 5. Iulian Năstac Computers Programming Course 5 Iulian Năstac Recap from previous course Classification of the programming languages High level (Ada, Pascal, Fortran, etc.) programming languages with strong abstraction

More information

211: Computer Architecture Summer 2016

211: Computer Architecture Summer 2016 211: Computer Architecture Summer 2016 Liu Liu Topic: C Programming Data Representation I/O: - (example) cprintf.c Memory: - memory address - stack / heap / constant space - basic data layout Pointer:

More information

Introduction to file management

Introduction to file management 1 Introduction to file management Some application require input to be taken from a file and output is required to be stored in a file. The C language provides the facility of file input-output operations.

More information

Reading lines from text files

Reading lines from text files Overview Lecture 20: String Processing Reading lines from text files Processing strings Breaking a string into tokens Comparing strings for equality Converting strings to numerals Reading lines from text

More information

Using the fprintf command to save output to a file.

Using the fprintf command to save output to a file. Using the fprintf command to save output to a file. In addition to displaying output in the Command Window, the fprintf command can be used for writing the output to a file when it is necessary to save

More information

Advanced C Programming Topics

Advanced C Programming Topics Introductory Medical Device Prototyping Advanced C Programming Topics, http://saliterman.umn.edu/ Department of Biomedical Engineering, University of Minnesota Operations on Bits 1. Recall there are 8

More information

Interactive MATLAB use. Often, many steps are needed. Automated data processing is common in Earth science! only good if problem is simple

Interactive MATLAB use. Often, many steps are needed. Automated data processing is common in Earth science! only good if problem is simple Chapter 2 Interactive MATLAB use only good if problem is simple Often, many steps are needed We also want to be able to automate repeated tasks Automated data processing is common in Earth science! Automated

More information

Worksheet 6. Input and Output

Worksheet 6. Input and Output Worksheet 6. Input and Output Most programs (except those that run other programs) contain input or output. Both fortran and matlab can read and write binary files, but we will stick to ascii. It is worth

More information

SECTION 2: PROGRAMMING WITH MATLAB. MAE 4020/5020 Numerical Methods with MATLAB

SECTION 2: PROGRAMMING WITH MATLAB. MAE 4020/5020 Numerical Methods with MATLAB SECTION 2: PROGRAMMING WITH MATLAB MAE 4020/5020 Numerical Methods with MATLAB 2 Functions and M Files M Files 3 Script file so called due to.m filename extension Contains a series of MATLAB commands The

More information

Quick MATLAB Syntax Guide

Quick MATLAB Syntax Guide Quick MATLAB Syntax Guide Some useful things, not everything if-statement Structure: if (a = = = ~=

More information

There is also a more in-depth GUI called the Curve Fitting Toolbox. To run this toolbox, type the command

There is also a more in-depth GUI called the Curve Fitting Toolbox. To run this toolbox, type the command Matlab bootcamp Class 4 Written by Kyla Drushka More on curve fitting: GUIs Thanks to Anna (I think!) for showing me this. A very simple way to fit a function to your data is to use the Basic Fitting GUI.

More information

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 12

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 12 BIL 104E Introduction to Scientific and Engineering Computing Lecture 12 Files v.s. Streams In C, a file can refer to a disk file, a terminal, a printer, or a tape drive. In other words, a file represents

More information

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming 1 Literal Constants Definition A literal or a literal constant is a value, such as a number, character or string, which may be assigned to a variable or a constant. It may also be used directly as a function

More information

DECLARATIONS. Character Set, Keywords, Identifiers, Constants, Variables. Designed by Parul Khurana, LIECA.

DECLARATIONS. Character Set, Keywords, Identifiers, Constants, Variables. Designed by Parul Khurana, LIECA. DECLARATIONS Character Set, Keywords, Identifiers, Constants, Variables Character Set C uses the uppercase letters A to Z. C uses the lowercase letters a to z. C uses digits 0 to 9. C uses certain Special

More information

Number Systems. Binary Numbers. Appendix. Decimal notation represents numbers as powers of 10, for example

Number Systems. Binary Numbers. Appendix. Decimal notation represents numbers as powers of 10, for example Appendix F Number Systems Binary Numbers Decimal notation represents numbers as powers of 10, for example 1729 1 103 7 102 2 101 9 100 decimal = + + + There is no particular reason for the choice of 10,

More information

Basic data types. Building blocks of computation

Basic data types. Building blocks of computation Basic data types Building blocks of computation Goals By the end of this lesson you will be able to: Understand the commonly used basic data types of C++ including Characters Integers Floating-point values

More information

Matlab Input/Output. Goal of this section Learn user-controlled Input Output Read/write data files (optional) Input/output 5-

Matlab Input/Output. Goal of this section Learn user-controlled Input Output Read/write data files (optional) Input/output 5- Matlab Input/Output Goal of this section Learn user-controlled Input Output Read/write data files (optional) 85-232 Input/output 5-1 Input: initialization >> a=[-1 0 0 ; 1 1 0] >> b=[1, 2, 3; 2:4] >> b=[1,

More information

12 whereas if I terminate the expression with a semicolon, the printed output is suppressed.

12 whereas if I terminate the expression with a semicolon, the printed output is suppressed. Example 4 Printing and Plotting Matlab provides numerous print and plot options. This example illustrates the basics and provides enough detail that you can use it for typical classroom work and assignments.

More information

COMP2611: Computer Organization. Data Representation

COMP2611: Computer Organization. Data Representation COMP2611: Computer Organization Comp2611 Fall 2015 2 1. Binary numbers and 2 s Complement Numbers 3 Bits: are the basis for binary number representation in digital computers What you will learn here: How

More information

System Software Experiment 1 Lecture 7

System Software Experiment 1 Lecture 7 System Software Experiment 1 Lecture 7 spring 2018 Jinkyu Jeong ( jinkyu@skku.edu) Computer Systems Laboratory Sungyunkwan University http://csl.skku.edu SSE3032: System Software Experiment 1, Spring 2018

More information

Chapter 9. Above: An early computer input/output device on the IBM 7030 (STRETCH)

Chapter 9. Above: An early computer input/output device on the IBM 7030 (STRETCH) Chapter 9 Above: An early computer input/output device on the IBM 7030 (STRETCH) http://computer-history.info/page4.dir/pages/ibm.7030.stretch.dir/ Io One of the moon s of Jupiter (A Galilean satellite)

More information

Strings and I/O functions

Strings and I/O functions Davies: Computer Vision, 5 th edition, online materials Matlab Tutorial 2 1 Strings and I/O functions 1. Introduction It is the purpose of these online documents to provide information on Matlab and its

More information

File IO and command line input CSE 2451

File IO and command line input CSE 2451 File IO and command line input CSE 2451 File functions Open/Close files fopen() open a stream for a file fclose() closes a stream One character at a time: fgetc() similar to getchar() fputc() similar to

More information

Variables and Literals

Variables and Literals C++ By 4 EXAMPLE Variables and Literals Garbage in, garbage out! To understand data processing with C++, you must understand how C++ creates, stores, and manipulates data. This chapter teaches you how

More information

2/12/17. Goals of this Lecture. Historical context Princeton University Computer Science 217: Introduction to Programming Systems

2/12/17. Goals of this Lecture. Historical context Princeton University Computer Science 217: Introduction to Programming Systems Princeton University Computer Science 217: Introduction to Programming Systems The C Programming Language Part 1 For Your Amusement C is quirky, flawed, and an enormous success. While accidents of history

More information

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University ITC213: STRUCTURED PROGRAMMING Bhaskar Shrestha National College of Computer Studies Tribhuvan University Lecture 07: Data Input and Output Readings: Chapter 4 Input /Output Operations A program needs

More information

SECTION 1: INTRODUCTION. ENGR 112 Introduction to Engineering Computing

SECTION 1: INTRODUCTION. ENGR 112 Introduction to Engineering Computing SECTION 1: INTRODUCTION ENGR 112 Introduction to Engineering Computing 2 Course Overview What is Programming? 3 Programming The implementation of algorithms in a particular computer programming language

More information

Introduction to Matlab

Introduction to Matlab Introduction to Matlab 1 Outline: What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators (Arithmetic, relational, logical ) Display Facilities Flow Control Using of M-File Writing User

More information

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved. C How to Program, 6/e 1992-2010 by Pearson Education, Inc. An important part of the solution to any problem is the presentation of the results. In this chapter, we discuss in depth the formatting features

More information

CpSc 101, Fall 2015 Lab7: Image File Creation

CpSc 101, Fall 2015 Lab7: Image File Creation CpSc 101, Fall 2015 Lab7: Image File Creation Goals Construct a C language program that will produce images of the flags of Poland, Netherland, and Italy. Image files Images (e.g. digital photos) consist

More information

Princeton University Computer Science 217: Introduction to Programming Systems The C Programming Language Part 1

Princeton University Computer Science 217: Introduction to Programming Systems The C Programming Language Part 1 Princeton University Computer Science 217: Introduction to Programming Systems The C Programming Language Part 1 C is quirky, flawed, and an enormous success. While accidents of history surely helped,

More information

MATLAB 7 Data Import and Export

MATLAB 7 Data Import and Export MATLAB 7 Data Import and Export How to Contact The MathWorks www.mathworks.com Web comp.soft-sys.matlab Newsgroup www.mathworks.com/contact_ts.html Technical Support suggest@mathworks.com bugs@mathworks.com

More information

Number Systems, Scalar Types, and Input and Output

Number Systems, Scalar Types, and Input and Output Number Systems, Scalar Types, and Input and Output Outline: Binary, Octal, Hexadecimal, and Decimal Numbers Character Set Comments Declaration Data Types and Constants Integral Data Types Floating-Point

More information

Chapter 3: Programming with MATLAB

Chapter 3: Programming with MATLAB Chapter 3: Programming with MATLAB Choi Hae Jin Chapter Objectives q Learning how to create well-documented M-files in the edit window and invoke them from the command window. q Understanding how script

More information

Standard 11. Lesson 9. Introduction to C++( Up to Operators) 2. List any two benefits of learning C++?(Any two points)

Standard 11. Lesson 9. Introduction to C++( Up to Operators) 2. List any two benefits of learning C++?(Any two points) Standard 11 Lesson 9 Introduction to C++( Up to Operators) 2MARKS 1. Why C++ is called hybrid language? C++ supports both procedural and Object Oriented Programming paradigms. Thus, C++ is called as a

More information

Exercise: Using Numbers

Exercise: Using Numbers Exercise: Using Numbers Problem: You are a spy going into an evil party to find the super-secret code phrase (made up of letters and spaces), which you will immediately send via text message to your team

More information

Chapter 6 Primitive types

Chapter 6 Primitive types Chapter 6 Primitive types Lesson page 6-1. Primitive types Question 1. There are an infinite number of integers, so it would be too ineffient to have a type integer that would contain all of them. Question

More information

C mini reference. 5 Binary numbers 12

C mini reference. 5 Binary numbers 12 C mini reference Contents 1 Input/Output: stdio.h 2 1.1 int printf ( const char * format,... );......................... 2 1.2 int scanf ( const char * format,... );.......................... 2 1.3 char

More information

Programming for Engineers Introduction to C

Programming for Engineers Introduction to C Programming for Engineers Introduction to C ICEN 200 Spring 2018 Prof. Dola Saha 1 Simple Program 2 Comments // Fig. 2.1: fig02_01.c // A first program in C begin with //, indicating that these two lines

More information

Number Systems Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur Number Representation

Number Systems Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur Number Representation Number Systems Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur 1 Number Representation 2 1 Topics to be Discussed How are numeric data items actually

More information

CSCE C. Lab 10 - File I/O. Dr. Chris Bourke

CSCE C. Lab 10 - File I/O. Dr. Chris Bourke CSCE 155 - C Lab 10 - File I/O Dr. Chris Bourke Prior to Lab Before attending this lab: 1. Read and familiarize yourself with this handout. 2. Review the following free textbook resources: http://en.wikibooks.org/wiki/c_programming/file_io

More information

Introduction to MATLAB Programming. Chapter 3. Linguaggio Programmazione Matlab-Simulink (2017/2018)

Introduction to MATLAB Programming. Chapter 3. Linguaggio Programmazione Matlab-Simulink (2017/2018) Introduction to MATLAB Programming Chapter 3 Linguaggio Programmazione Matlab-Simulink (2017/2018) Algorithms An algorithm is the sequence of steps needed to solve a problem Top-down design approach to

More information

Strings Additions File I/O. Strings and I/O. K. Cooper 1. 1 Department of Mathematics. Washington State University. Strings

Strings Additions File I/O. Strings and I/O. K. Cooper 1. 1 Department of Mathematics. Washington State University. Strings and I/O K. 1 1 Department of Mathematics 2018 Character We have already used character strings a few times. We have seen them as simple collections of characters, surrounded by quotation marks. e.g. cat,

More information

C File System File Functions EXPERIMENT 1.2

C File System File Functions EXPERIMENT 1.2 C File System File Functions EXPERIMENT 1.2 Propose of the experiment Continue from previous experiment to be familiar with CCS environment Write a C language file input / output (CIO) program to read

More information

PART 2 DEVELOPING M-FILES

PART 2 DEVELOPING M-FILES PART 2 DEVELOPING M-FILES Presenter: Dr. Zalilah Sharer 2018 School of Chemical and Energy Engineering Universiti Teknologi Malaysia 17 September 2018 Topic Outcomes Week Topic Topic Outcomes 6-8 Creating

More information

Spring 2010 Instructor: Michele Merler.

Spring 2010 Instructor: Michele Merler. Spring 2010 Instructor: Michele Merler http://www1.cs.columbia.edu/~mmerler/comsw3101-2.html Type from command line: matlab -nodisplay r command Tells MATLAB not to initialize the visual interface NOTE:

More information

UNIT IV-2. The I/O library functions can be classified into two broad categories:

UNIT IV-2. The I/O library functions can be classified into two broad categories: UNIT IV-2 6.0 INTRODUCTION Reading, processing and writing of data are the three essential functions of a computer program. Most programs take some data as input and display the processed data, often known

More information

IT 1204 Section 2.0. Data Representation and Arithmetic. 2009, University of Colombo School of Computing 1

IT 1204 Section 2.0. Data Representation and Arithmetic. 2009, University of Colombo School of Computing 1 IT 1204 Section 2.0 Data Representation and Arithmetic 2009, University of Colombo School of Computing 1 What is Analog and Digital The interpretation of an analog signal would correspond to a signal whose

More information

Final Labs and Tutors

Final Labs and Tutors ICT106 Fundamentals of Computer Systems - Topic 2 REPRESENTATION AND STORAGE OF INFORMATION Reading: Linux Assembly Programming Language, Ch 2.4-2.9 and 3.6-3.8 Final Labs and Tutors Venue and time South

More information

Chapter 2, Part I Introduction to C Programming

Chapter 2, Part I Introduction to C Programming Chapter 2, Part I Introduction to C Programming C How to Program, 8/e, GE 2016 Pearson Education, Ltd. All rights reserved. 1 2016 Pearson Education, Ltd. All rights reserved. 2 2016 Pearson Education,

More information

1

1 0 1 4 Because a refnum is a temporary pointer to an open object, it is valid only for the period during which the object is open. If you close the object, LabVIEW disassociates the refnum with the object,

More information

MATLAB Part 1. Introduction

MATLAB Part 1. Introduction MATLAB Part 1 Introduction MATLAB is problem solving environment which provides engineers and scientists an easy-to-use platform for a wide range of computational problems. In general, it is useful for

More information

Numerical Analysis First Term Dr. Selcuk CANKURT

Numerical Analysis First Term Dr. Selcuk CANKURT ISHIK UNIVERSITY FACULTY OF ENGINEERING and DEPARTMENT OF COMPUTER ENGINEERING Numerical Analysis 2017-2018 First Term Dr. Selcuk CANKURT selcuk.cankurt@ishik.edu.iq Textbook Main Textbook MATLAB for Engineers,

More information

Matlab course at. P. Ciuciu 1,2. 1: CEA/NeuroSpin/LNAO 2: IFR49

Matlab course at. P. Ciuciu 1,2. 1: CEA/NeuroSpin/LNAO 2: IFR49 Matlab course at NeuroSpin P. Ciuciu 1,2 philippe.ciuciu@cea.fr www.lnao.fr 1: CEA/NeuroSpin/LNAO 2: IFR49 Mar 5, 2009 Outline 2/9 Lesson0: Getting started: environment,.m and.mat files Lesson I: Scalar,

More information

Getting started with Java

Getting started with Java Getting started with Java Magic Lines public class MagicLines { public static void main(string[] args) { } } Comments Comments are lines in your code that get ignored during execution. Good for leaving

More information

1/25/2018. ECE 220: Computer Systems & Programming. Write Output Using printf. Use Backslash to Include Special ASCII Characters

1/25/2018. ECE 220: Computer Systems & Programming. Write Output Using printf. Use Backslash to Include Special ASCII Characters University of Illinois at Urbana-Champaign Dept. of Electrical and Computer Engineering ECE 220: Computer Systems & Programming Review: Basic I/O in C Allowing Input from the Keyboard, Output to the Monitor

More information

CMPT 125: Lecture 3 Data and Expressions

CMPT 125: Lecture 3 Data and Expressions CMPT 125: Lecture 3 Data and Expressions Tamara Smyth, tamaras@cs.sfu.ca School of Computing Science, Simon Fraser University January 3, 2009 1 Character Strings A character string is an object in Java,

More information

Lesson 2 Characteristics of Good Code Writing (* acknowledgements to Dr. G. Spinelli, New Mexico Tech, for a substantial portion of this lesson)

Lesson 2 Characteristics of Good Code Writing (* acknowledgements to Dr. G. Spinelli, New Mexico Tech, for a substantial portion of this lesson) T-01-13-2009 GLY 6932/6862 Numerical Methods in Earth Sciences Spring 2009 Lesson 2 Characteristics of Good Code Writing (* acknowledgements to Dr. G. Spinelli, New Mexico Tech, for a substantial portion

More information

CME 192: Introduction to Matlab

CME 192: Introduction to Matlab CME 192: Introduction to Matlab Matlab Basics Brett Naul January 15, 2015 Recap Using the command window interactively Variables: Assignment, Identifier rules, Workspace, command who and whos Setting the

More information

TCL - STRINGS. Boolean value can be represented as 1, yes or true for true and 0, no, or false for false.

TCL - STRINGS. Boolean value can be represented as 1, yes or true for true and 0, no, or false for false. http://www.tutorialspoint.com/tcl-tk/tcl_strings.htm TCL - STRINGS Copyright tutorialspoint.com The primitive data-type of Tcl is string and often we can find quotes on Tcl as string only language. These

More information

Microsoft Excel 2010 Basics

Microsoft Excel 2010 Basics Microsoft Excel 2010 Basics Starting Word 2010 with XP: Click the Start Button, All Programs, Microsoft Office, Microsoft Excel 2010 Starting Word 2010 with 07: Click the Microsoft Office Button with the

More information

The Design of C: A Rational Reconstruction (cont.)

The Design of C: A Rational Reconstruction (cont.) The Design of C: A Rational Reconstruction (cont.) 1 Goals of this Lecture Recall from last lecture Help you learn about: The decisions that were available to the designers of C The decisions that were

More information

M1 Computers and Data

M1 Computers and Data M1 Computers and Data Module Outline Architecture vs. Organization. Computer system and its submodules. Concept of frequency. Processor performance equation. Representation of information characters, signed

More information

CPE 101, reusing/mod slides from a UW course (used by permission) Lecture 5: Input and Output (I/O)

CPE 101, reusing/mod slides from a UW course (used by permission) Lecture 5: Input and Output (I/O) CPE 101, reusing/mod slides from a UW course (used by permission) Lecture 5: Input and Output (I/O) Overview (5) Topics Output: printf Input: scanf Basic format codes More on initializing variables 2000

More information

CSCI 6610: Review. Chapter 7: Numbers Chapter 8: Characters Chapter 11 Pointers

CSCI 6610: Review. Chapter 7: Numbers Chapter 8: Characters Chapter 11 Pointers ... 1/27 CSCI 6610: Review Chapter 7: Numbers Chapter 8: Characters Chapter 11 Pointers Alice E. Fischer February 1, 2016 ... 2/27 Outline The Trouble with Numbers The char Data Types Pointers ... 3/27

More information