


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
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
1 / 4
This page cannot be seen from the preview
Don't miss anything!
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
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
[atomNums names symbols masses] = ... textread( ' elements.txt' , ' %u %s %s %f ' );
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
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
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
1 hydrogen H 1. 2 helium He 4. 3 lithium Li 6. 4 beryllium Be 9. …
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
Cell arrays 16-
Parsing the string of html code
Cell arrays 16-
Other file formats