Fprintf and other examples. Save command >> !ls >> a = 3; >> save afile a >> !ls afile.mat >> !dir Directory of C:\MATLAB6p5\work\save 11/21/2003 09:30.

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 M ATLAB 4 Useful stuff for real programming Ian Brooks Institute for Climate & Atmospheric Science School of Earth & Environment
Introduction to MATLAB for Biomedical Engineering BME 1008 Introduction to Biomedical Engineering FIU, Spring 2015 Lesson 2: Element-wise vs. matrix operations.
BITS Pilani, Pilani Campus TA C252 Computer Programming - II Vikas Singh File Handling.
M AT L AB Reading & Writing Files. Low-level file input and output is very similar to that in 'C', but with inherent vectorization. Files are opened with.
MATLAB Input-Output Statements Nafees Ahmed Asstt. Professor, EE Deptt DIT, DehraDun.
Nov. 28, 2005 Lecture 14 - By Paul Lin 1 CPET 190 Problem Solving with MATLAB Lecture 14 Professor Paul Lin
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.
Lesson 2: Basic Output You’ve got data……now what???
Lesson 2: Basic Output You’ve got data……now what???
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 =
CIS 101: Computer Programming and Problem Solving Lecture 4 Usman Roshan Department of Computer Science NJIT.
Input/Output Functions Selim Aksoy Bilkent University Department of Computer Engineering
Fall 2006AE6382 Design Computing1 Matlab File & Directory Management Learning Objectives Define file input and output terminology Compare high and low.
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)
2 $ command Command Line Options ls –a –l hello hi Command Arguments.
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.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 21P. 1Winter Quarter MATLAB: Structures.
January 24, 2005 Lecture 3 - By P. Lin 1 CPET 190 Lecture 3 Problem Solving with MATLAB
Chapter 4 MATLAB Programming Combining Loops and Logic Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
MATLAB File Management. MATLAB User File Management Matlab provides a group of commands to manage user files. For more information, type help iofun. pwd.
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.
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.
MEGN 536 – Computational Biomechanics MATLAB: Getting Started Prof. Anthony J. Petrella Computational Biomechanics Group.
Matlab Training Session 10: Loading Binary Data Course Website: Training Sessions.htm.
File Handling Spring 2013Programming and Data Structure1.
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)
Chapter 8 : Binary Data Files1 Binary Data Files CHAPTER 8.
Using Text Files in Excel File I/O Methods. Working With Text Files A file can be accessed in any of three ways: –Sequential access: By far the most common.
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.
Digital Image Processing Introduction to MATLAB. Background on MATLAB (Definition) MATLAB is a high-performance language for technical computing. The.
Input Output Garbage In, Garbage Out. Outline Announcements: –Homework III: due Today. by 5, by Discuss on Friday. –Homework IV: on web, due following.
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
Introduction to Matlab Patrice Koehl Department of Biological Sciences National University of Singapore
THE C PROGRAMMING ENVIRONMENT. Four parts of C environment  Main menu  Editor status line and edit window  Compiler message window  “Hot Keys” quick.
File Access Storage of data in variables and arrays is temporary—such data is lost when a program terminates. Files are used for permanent retention of.
1 CSC103: Introduction to Computer and Programming Lecture No 28.
Using and Programming with MATLAB as an Engineering Tool [ Part III ]
Files. FILE * u In C, we use a FILE * data type to access files. u FILE * is defined in /usr/include/stdio.h u An example: #include int main() { FILE.
Input Output Garbage In, Garbage Out. Outline Announcements: –HWII solutions on web soon –Homework III: due Wednesday Advanced ASCII Binary Basics Cell-arrays.
Lecture 20: C File Processing. Why Using Files? Storage of data in variables and arrays is temporary Data lost when a program terminates. Files are used.
SUNY-New Paltz Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz “Lecture 11”
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.
Machine Learning Octave tutorial 데이터 마이닝 박 영 택.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Input Output Garbage In, Garbage Out. Outline Announcements: –Homework III: due Wednesday Advanced ASCII Binary Basics Filesystem Fun.
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,
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
MATLAB: Structures and File I/O
Introduction to MATLAB
Advanced Data Import & Export Jeff Henrikson
Lecture 13 Input/Output Files.
Matlab Training Session 5: Importing Data
Note on Indexing of Array Elements
Input/Output Functions
Files.
Input/Output Functions
CSc 352 File I/O Saumya Debray Dept. of Computer Science
Presentation transcript:

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 AM. 11/21/ :30 AM.. 11/21/ :30 AM 184 afile.mat 1 File(s) 184 bytes

Load Command >> clear a; >> whos >> load afile.mat >> whos Name Size Bytes Class a 1x1 8 double array Grand total is 1 element using 8 bytes

Load/save entire workspace >> v = 1:5; >> m = ones(2, 3); >> who Your variables are: a m v >> save allfile >> !ls afile.mat allfile.mat >> clear >> whos >> load allfile.mat >> who Your variables are: a m v

Ascii Format >> save -ascii allfile.dat >> !more allfile.dat e e e e e e e e e+000 >> clear >> load -ascii allfile.dat ??? Error using ==> load Number of columns on line 2 of ASCII file C:\MATLAB6p5\work\save\allfile.dat must be the same as previous lines. >> who >> m = ones(2, 3); >> save -ascii m.dat >> clear >> load -ascii m.dat >> m m = 1 1 1

File Example filename = input('Enter file name: ','s'); out_array = randn(1,10000); % Open the output file for writing. [fid,msg] = fopen(filename,'w'); % Was the open successful? if fid > 0 % Write the output data. count = fwrite(fid,out_array,'float64'); disp([int2str(count) ' values written...']); % Close the file status = fclose(fid); else % Output file open failed. Display message. disp(msg); end

fopen fid = fopen(filename,permission) opens the file filename in the mode specified by permission. permission can be: 'r‘ Open file for reading (default). 'w‘ Open file, or create new file, for writing; discard existing contents, if any. 'a‘ Open file, or create new file, for writing; append data to the end of the file. 'r+‘ Open file for reading and writing. 'w+‘ Open file, or create a new file, for reading and writing; discard existing contents, if any. 'a+‘ Open file, or create new file, for reading and writing; append data to the end of the file.

Reading Data Back % Now try to recover the data. Open the file for reading. [fid,msg] = fopen(filename,'r'); if fid > 0 % Read the input data. [in_array, count] = fread(fid,[ ],'float64'); disp([int2str(count) ' values read...']); % Close the file status = fclose(fid); else % Input file open failed. Display message. disp(msg); end

fread function [A, COUNT] = FREAD(FID,SIZE,PRECISION) reads binary data from the specified file and writes it into matrix A. Optional output argument COUNT returns the number of elements successfully read. FID is an integer file identifier obtained from FOPEN. The SIZE argument is optional; if not specified, the entire file is read; if specified, valid entries 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. N can be inf, but M can't.

fprintf example >> fid = fopen('grades', 'w'); >> fprintf(fid, '%d', randperm(40)); >> fclose(fid); >> !more grades

fprintf example >> fid = fopen('grades', 'w'); >> fprintf(fid, '%d ', randperm(40)); >> fclose(fid); >> !more grades

fscanf example >> fid = fopen('grades', 'r'); >> grs = fscanf(fid, '%d', [1 40]) grs = Columns 1 through … >> grs = fscanf(fid, '%d', [5 4]) grs = []