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,

Slides:



Advertisements
Similar presentations
Introduction to M ATLAB 4 Useful stuff for real programming Ian Brooks Institute for Climate & Atmospheric Science School of Earth & Environment
Advertisements

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.
DATA RETRIEVAL AND GUI CREATION DAVID COOPER SUMMER 2014.
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.
Introduction to MATLAB Northeastern University: College of Computer and Information Science Co-op Preparation University (CPU) 10/27/2003.
EGR 106 – Truss Design Project (cont.) Truss design programs Graphical interface tools in Matlab Saving and loading data Formatted output Project Assignment.
Lesson 2: Basic Output You’ve got data……now what???
Input/Output Functions Selim Aksoy Bilkent University Department of Computer Engineering
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
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)
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.
AN ENGINEER’S GUIDE TO MATLAB
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.
Cell Arrays 1.Definition 2.Creating Cell Arrays 3.Referencing Cell Arrays 4.Augmenting Cell Arrays 5.Use of Cell Arrays 1.
Introduction to File I/O High-Level Functions 1.Data files 2."High level" File I/O 3.dlmread() 4.xlsread() 1.
Matlab Training Session 10: Loading Binary Data Course Website: Training Sessions.htm.
FILE I/O: Low-level General Ideas. High vs. low-level Opening a file
CMPS 1371 Introduction to Computing for Engineers FILE Input / Output.
File I/O High-Level Functions 1. Definition 2. Is a High-Level function appropriate? 3. xlsread() 4. dlmread() 1.
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
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)
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.
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.
MATLAB for Engineers, by Holly Moore. ISBN © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is.
1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
Early File I/O To help you get started with your final project 1. Definition of “high level” 2. Is using a High Level function appropriate? 3. xlsread()
File Operations in Matlab Load / Save *.dat, *.mat vs. -ascii fopen /fclose.
Using and Programming with MATLAB as an Engineering Tool [ Part III ]
SUNY-New Paltz Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz “Lecture 11”
Sundermeyer MAR 999 Spring Laboratory in Oceanography: Data and Methods MAR599, Spring 2009 Miles A. Sundermeyer Data Handling in Matlab.
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.
An Introduction to Programming in Matlab Emily Blumenthal
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()
Reading and Writing Data Files
EEE 161 Applied Electromagnetics
Prof. Mark Glauser Created by: David Marr
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.
Shell Scripting March 1st, 2004 Class Meeting 7.
Have you signed up (or had) your meeting?
IMPORTING AND EXPORTING DATA
Other Kinds of Arrays Chapter 11
Other Kinds of Arrays Chapter 11
Scripts & Functions Scripts and functions are contained in .m-files
Outline Matlab tutorial How to start and exit Matlab Matlab basics.
MATLAB: Structures and File I/O
Jeff Henrikson Lecture 11 Data Import & Export Jeff Henrikson 1.
Introduction to MATLAB
Advanced Data Import & Export Jeff Henrikson
Lecture 13 Input/Output Files.
Beginning C Lecture 11 Lecturer: Dr. Zhao Qinpei
Matlab Training Session 5: Importing Data
Introduction to MATLAB
funCTIONs and Data Import/Export
Files Handling In today’s lesson we will look at:
Note on Indexing of Array Elements
Introduction to MATLAB
Input/Output Functions
Islamic University of Gaza
Files.
Working With Data.
Functions continued.
Topics Introduction to File Input and Output
Input/Output Functions
Excel Tips & Tricks July 18, 2019.
Presentation transcript:

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, if you want to read another kind of file, or create a file that can be read outside of Matlab, you must deal with files directly Matlab can also handle text files and and binary files (as well as excel files – see xlsread and xlswrite)

Working with files Introducing fopen() and fclose() not always necessary but… … general plan for working with files: >> help fopen % check bin vs. text mode & corresponding permission codes fopen() <read from file or write to file> fclose()

Opening files fid = fopen(filename, permission) number returned by fopen which you will use to refer to this file fid = fopen(filename, permission) string with name of file or full path to file if it’s not in the current directory string containing code that determines what Matlab is allowed to do with this file

Writing to text files >> myFileID = fopen('testfile.txt','w') myFileID = 3 >> x = 100; >> fprintf(myFileID,'X is equal to %d\n',x); >> fclose(myFileID); >> fopen('/usr/bin/test.txt','w') ans = -1 If fopen() returns -1 then it has failed to open the file >> x = [1:10]; >> y = x .^3; >> myExponentsFile = fopen('e.txt','w'); >> fprintf(myExponentsFile,'%d %d\n',[x;y]); >> fclose(myExponentsFile)

Writing to text files Other ways to write (matrices) to files: csvwrite() % write a comma-separated value file. dlmwrite() % write ASCII delimited file data=rand(10,5); dlmwrite('data1.csv',data); dlmwrite('data2.tab',data,'\t'); >> x = rand(5) x = 0.0855 0.7303 0.9631 0.6241 0.0377 0.2625 0.4886 0.5468 0.6791 0.8852 0.8010 0.5785 0.5211 0.3955 0.9133 0.0292 0.2373 0.2316 0.3674 0.7962 0.9289 0.4588 0.4889 0.9880 0.0987 >> csvwrite('randomvalues.csv',x) >> clear all >> x = csvread('randomvalues.csv') 0.9288 0.4588 0.4889 0.9880 0.0987

Reading from text files to read numeric data from a text file: load(‘filename’) % can also write such data with save csvread(‘filename’) % read comma separated file dlmread(‘filename’) % read ASCII delimited file to read strings from a text file: fgets(fid) % used with files that contain newline characters fgetl(fid) % here the line terminator is not included note: these commands read one line only so use ischar in a while loop to read the whole text To read both or even mixed stuff from a text file: importdata(‘filename’) % can have non-numeric headers A= fscanf(fid,FORMAT) % can also determine range/shape (works columnwise!) textscan(fid, FORMAT) % reads data into a cell array >> help type % prints a text file in the command window

Reading from text files - textscan >> help textscan textscan Read formatted data from text file or string. C = textscan(FID,'FORMAT') reads data from an open text file identified by FID into cell array C. Use FOPEN to open the file and obtain FID. The FORMAT is a string of conversion specifiers enclosed in single quotation marks. The number of specifiers determines the number of cells in the cell array C. For more information, see "Format Options."

Reading from text files - textscan Contents of "log.txt": >> logFID = fopen('log.txt'); >> data = textscan(logFID,'%s %f %f %f %f %f %f') data = Columns 1 through 5 {9x1 cell} [9x1 double] [9x1 double] [9x1 double] [9x1 double] Columns 6 through 7 [9x1 double] [9x1 double]

Reading from text files - textscan >> subjectcodes = data{1} subjectcodes = 'SM01' 'SM02' 'SM03' 'SM04' 'SM05' 'SM06' 'SM07' 'SM08' 'SM09' >> fclose(logFID); >>

Binary-files (data files, e.g. ‘.dat’) to write an array A into a binary file: fid=fopen(filename,’w’) fwrite(fid, A ,’double’) % can have other types, including ‘char’ fclose(fid) help fwrite to read an array A from a binary file: fid=fopen(filename,’r’) fread(fid, inf, ‘double’) % inf means read all - read/write range can be specified help fread problem: fwrite columnwise produces a single column so if I want to retrieve my original matrix, I need to include its dimensions inside the file (first the #dims, then the dims themselves and then the actual data).