Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

CS112 Scientific Computation: Reading and Writing Text Files using Cell Arrays in MATLAB, Study notes of Computer Science

This document from the cs112 scientific computation course at wellesley college explains how to read and write text files into cell arrays in matlab. It covers reading text files with the textread function, formatted data, and writing data to text files. Examples are provided for numerical data and strings.

Typology: Study notes

Pre 2010

Uploaded on 08/16/2009

koofers-user-wi0
koofers-user-wi0 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CS112 Scientific Computation
Department of Computer Science
Wellesley College
Storing it away for safe keeping
Reading and writing text files
Cell arrays 16-2
Reading a text file into a cell array
Large amounts of text data can be stored in text files, with a .txt
extension, e.g. mobydick.txt
Successive lines of a text file can be read into the cells of a cell
array using the textread function.
>> lines = textread('mobydick.txt','%s','delimiter', '\n');
>> lines{1}
ans =
Call me Ishmael. Some years ago – never mind how long precisely - having
>> lines{2}
ans =
little or no money in my purse, and nothing particular to interest me on shore,
Call me Ishmael. Some years ago – never mind how long precisely – having
little or no money in my purse, and nothing particular to interest me on shor e,
I thought I would sail about a little and see the watery part of the world. It
is a way I have of driving off the spleen a nd regulating the circulation…
Cell arrays 16-3
More on textread
Suppose we have a text file elements.txt that contains
a combination of numerical data and strings, in a
fixed format
1 hydrogen H 1.01
2 helium He 4.00
3 lithium Li 6.94
4 beryllium Be 9.01
5 boron B 10.81
6 carbon C 12.01
7 nitrogen N 14.01
8 oxygen O 16.00
9 fluorine F 19.00
10 neon Ne 20.18
...
Cell arrays 16-4
Reading formatted data
The format string can incorporate other types:
>> [atomNums names symbols masses] = ...
textread('elements.txt', '%u %s %s %f');
Recall our text file elements.txt with numerical data and strings:
>> atomNums'
1 2 3 4 5 6 7 8 9 10 …
>> names'
'hydrogen' 'helium' 'lithium' 'beryllium' 'boron' 'carbon'
>> symbols'
'H''He''Li''Be''B''C''N''O''F''Ne'
>> masses'
1.0100 4.0000 6.9400 9.0100 10.8100 12.0100 14.0100 …
format string:
%u integer
%s string
%f float
'is the transpose operator
pf3
pf4

Partial preview of the text

Download CS112 Scientific Computation: Reading and Writing Text Files using Cell Arrays in MATLAB and more Study notes Computer Science in PDF only on Docsity!

CS112 Scientific Computation

Department of Computer Science Wellesley College

Storing it away for safe keeping

Reading and writing text files

Cell arrays 16-

Reading a text file into a cell array

Large amounts of text data can be stored in text files, with a .txt extension, e.g. mobydick.txt

Successive lines of a text file can be read into the cells of a cell array using the textread function.

lines = textread('mobydick.txt', '%s', 'delimiter', '\n'); lines{1} ans = Call me Ishmael. Some years ago – never mind how long precisely - having lines{2} ans = little or no money in my purse, and nothing particular to interest me on shore,

Call me Ishmael. Some years ago – never mind how long precisely – having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world. It is a way I have of driving off the spleen and regulating the circulation…

Cell arrays 16-

More on textread

Suppose we have a text file elements.txt that contains

a combination of numerical data and strings, in a

fixed format

1 hydrogen H 1. 2 helium He 4. 3 lithium Li 6. 4 beryllium Be 9. 5 boron B 10. 6 carbon C 12. 7 nitrogen N 14. 8 oxygen O 16. 9 fluorine F 19. 10 neon Ne 20. ...

Cell arrays 16-

Reading formatted data

The format string can incorporate other types:

[atomNums names symbols masses] = ... textread( ' elements.txt' , ' %u %s %s %f ' );

Recall our text file elements.txt with numerical data and strings:

atomNums' 1 2 3 4 5 6 7 8 9 10 … names' 'hydrogen' 'helium' 'lithium' 'beryllium' 'boron' 'carbon' … symbols' 'H' 'He' 'Li' 'Be' 'B' 'C' 'N' 'O' 'F' 'Ne' … masses' 1.0100 4.0000 6.9400 9.0100 10.8100 12.0100 14.0100 …

format string: %u integer %s string %f float

' is the transpose operator

Cell arrays 16-

Sometimes life is not so simple…

name price quantity mr. potato head $3.29 80 slinky $1.29 120 hoola hoop $2.19 60 monopoly $3.89 50

Suppose we want to compute the total value of our toy

inventory, from a text file toys.txt with the following format:

lines = textread('toys.txt', '%s', 'delimiter', '\n'); totalValue = 0.0; for index = 2:length(lines) line = lines{index}; % extract price and quantity from each line totalValue = totalValue + price * quantity; end Cell arrays 16-

vice versa: Writing formatted data

Suppose you have data in the MATLAB workspace that you

want to store in a text file in a desired format

>> atomNums = [1 2 3 4 …];

>> names = {'hydrogen' 'helium‘ 'lithium' 'beryllium' …};

>> symbols = {'H' 'He' 'Li' 'Be' …};

>> masses =[1.01 4.00 6.94 9.01 …];

1 hydrogen H 1. 2 helium He 4. 3 lithium Li 6. 4 beryllium Be 9. …

elements.txt

Cell arrays 16-

Formatting strings with sprintf

sprintf('%u %s %s %f', 1, 'hydrogen', 'H', 1.01) ans = 1 Hydrogen H 1. sprintf( '%4u %12s %4s %8.2f', 1, 'hydrogen', 'H', 1.01) ans = 1 hydrogen H 1.

for i = 1: disp(sprintf('%4u %12s %4s %8.2f', atomNums(i), names{i}, … symbols{i}, masses(i))) end 1 hydrogen H 1. 2 helium He 4. 3 lithium Li 6. 4 beryllium Be 9.

Cell arrays 16-

Finally… Writing data to a text file

(1) Open file for writing

(2) Write text to file

(3) Close file

fid = fopen('elements.txt', 'w') ;

for i = 1:length(atomNums)

fprintf(fid, '%4u %12s %4s %8.2f \n', atomNums(i), …

names{i}, symbols{i}, masses(i));

end

fclose(fid);

Cell arrays 16-

Parsing the string of html code

A peek at the substring of str containing html code for creating the

first two rows of the table:

1880
13.88
1881
13.88

How can we get the numbers out?

see urlTest.m

Cell arrays 16-

Other file formats

MATLAB also supports a variety of

industry standard formats and

custom file formats

This allows MATLAB to exchange data

with other programs

Text: MAT, CSV, DLM, TAB

Scientific data: CDF, FITS, HDF

Spreadsheet: XLS, WK