Docsity

MATLAB: Strings, Formatted Output, and Files - Prof. Joh Calandrino, Study notes of Computer Science

Toggle sidebar
bg1
COMP 116
8 Oct 2007
Strings, Formatted output, and Files
Character strings: a MATLAB data type (Gilat, p.46-48)
An array or matrix can contain characters.
s = 'A string'; is shorthand for s = ['A' ' ' 's' 't' 'r' 'i' 'n' 'g'];
All array/vector operations, such as indexing, work as on vectors of numbers. Be aware that
comparing two strings with == or < works character by character, giving back a vector of
true/false values! Use strcmpi instead.
Some string operations (see also doc strings)
disp(s) displays a string to the console
num2str(x) Make a string of digits from a real number x.
int2str(i) Make a string of digits from an integer i.
str2double(s)Make a real number from a string of digits (scalar only).
str2num(s) Make a real number from a string of digits (can handle vectors).
blanks(n) n blank characters.
strvcat(s,t,…)Stack strings vertically, adding blanks to make all same length
deblank(s) Remove trailing blanks from a string s.
lower(s) converts uppercase characters in s to lowercase.
upper(s) converts uppercase characters in s to lowercase.
double(s) characters to ASCII codes.
char(v) ASCII codes to characters.
strcmp(s,t) compares strings s and t, returning 1 if they are identical
strcmpi(s,t) compares strings s and t, ignoring case.
strmatch(s,strarray)returns the row numbers in strarray that start with string s.
inline(s) makes a function of variable x from a string.
eval(s) Executes a string as a command.
Output: disp, fprintf (see doc fprintf, and Gilat 4.6)
With string operations, we can mix the printing of text and numbers:
disp(['The value of x is' num2str(x)]);
For more control, MATLAB has adopted the C-language formatted print statement
fprintf('The value of x is %f\n', x);
Here the %f means "print a floating point number." Other choices are integer (%d), string (%s),
scientific format (%e), scientific or normal, depending on magnitude (%g), …
There are many other options, the most important being control of spacing. E.g., If you want it to
print in a field of 12 spaces with 5 spaces after the decimal point, you could instead type %12.5f
The \n means go to the next line; if you leave that off, the next item printed will follow on the
same line. Other choices are \t for tab, \b for backspace, \\ for backslash, %% for one %
(percent), and '' (two single quotes) for one ' (single quote). (see doc fprintf)
fprintf can also output to files instead of the console by giving a file identifier.
pf2

Partial preview of the text

Download MATLAB: Strings, Formatted Output, and Files - Prof. Joh Calandrino and more Study notes Computer Science in PDF only on Docsity!

COMP 116

8 Oct 2007 Strings, Formatted output, and Files Character strings: a MATLAB data type (Gilat, p.46-48) An array or matrix can contain characters. s = 'A string'; is shorthand for s = ['A' ' ' 's' 't' 'r' 'i' 'n' 'g']; All array/vector operations, such as indexing, work as on vectors of numbers. Be aware that comparing two strings with == or < works character by character, giving back a vector of true/false values! Use strcmpi instead. Some string operations (see also doc strings) disp(s) displays a string to the console num2str(x) Make a string of digits from a real number x. int2str(i) Make a string of digits from an integer i. str2double(s)Make a real number from a string of digits (scalar only). str2num(s) Make a real number from a string of digits (can handle vectors). blanks(n) n blank characters. strvcat(s,t,…)Stack strings vertically, adding blanks to make all same length deblank(s) Remove trailing blanks from a string s. lower(s) converts uppercase characters in s to lowercase. upper(s) converts uppercase characters in s to lowercase. double(s) characters to ASCII codes. char(v) ASCII codes to characters. strcmp(s,t) compares strings s and t , returning 1 if they are identical strcmpi(s,t) compares strings s and t , ignoring case. strmatch(s,strarray)returns the row numbers in strarray that start with string s. inline(s) makes a function of variable x from a string. eval(s) Executes a string as a command. Output: disp, fprintf (see doc fprintf, and Gilat 4.6) With string operations, we can mix the printing of text and numbers: disp(['The value of x is' num2str(x)]); For more control, MATLAB has adopted the C-language formatted print statement fprintf('The value of x is %f\n', x); Here the %f means "print a floating point number." Other choices are integer (%d), string (%s), scientific format (%e), scientific or normal, depending on magnitude (%g), … There are many other options, the most important being control of spacing. E.g., If you want it to print in a field of 12 spaces with 5 spaces after the decimal point, you could instead type %12.5f The \n means go to the next line; if you leave that off, the next item printed will follow on the same line. Other choices are \t for tab, \b for backspace, \ for backslash, %% for one % (percent), and '' (two single quotes) for one ' (single quote). (see doc fprintf) fprintf can also output to files instead of the console by giving a file identifier.

File input MATLAB can read and write data to files using many different formats. Unfortunately, Gilat mentions files only briefly when he discusses file output in section 4.6. I'll describe a common idiom: how to read data from an ASCII (text) file that has data in specific columns. The protein databank files used in assignment #5 have this form. The basic structure of programs that take data from a file is usually something like this: filename = 'myfile.txt'; % or whatever the name should be fid = fopen(filename); % open the file for reading, giving it the id fid while ~feof(fid) % while there is a line remaining in the file line = fgetl(fid); % get the next line from the file <process the line, or certain pieces of it, as a string> end fclose(fid); % close the file when we are done with it Often the file name is passed in as a variable, so that the same function can be applied to different files without modification. The function feof(fid) returns true if we have already read the last line in the file, so this while loop continues until all lines have been read. A file will often have certain information in certain columns (a legacy of punched cards). E.g., the protein data bank files have some lines that start with 'ATOM ', which we can detect by if strcmpi(line(1:6), 'ATOM ') % note: two spaces after ATOM end Note the use of strcmpi, which compares strings and returns a single true or false. If we had just used ==, then we would get an array of true or false, since the strings are really arrays of characters. strcmpi also ignores upper/lower case distinctions. (see doc strcmpi), As you can see in the assignment #5 handout, columns 31–38 of a line starting with ATOM contain the x coordinate, which we could make into a number this way: x = str2num(line(31:38)); % extract x coordinate from line For more information see the documentation on fopen, fgetl,