# Matlab Text Output

Matlab is a package for solving complex numerical problems. It was never intended for use in sophisticated text handling. If we were serious about dealing with text, we would not be using Matlab. On the other hand, when we format our mathematical results for output, we need some text to explain and relate the results we print, hence Matlab does have some rudimentary tools for mixing text and numbers into something legible.

By now you know that Matlab prints out the name and value of any variable that is assigned in a statement that does not end with a semicolon. This is the simplest output method: just type the name of a variable whose value you want to see, and it will appear when the command runs. On the other hand, we frequently want to see numbers in the questions we pose as part of input statements, or we want computed values to appear in figure captions and such. For this, Matlab provides the C-like command sprintf.

The sprintf command has the form

sprintf(format string,variables)

The format string must be a character array enclosed in single quotes, that refers to numbers using a notation using percent signs that is borrowed from other programming languages that may be familiar to you. To display an integer, use the %d notation; to display a floating point number, use %f. This is best seen through some examples. If we were to write a script that looked like:

x=[1.2 2.2 3.9]; f=x.^2; for i=1:3, fvalue = sprintf('f(%d) = %f',i,f(i)); end

then when we run that script we would see

fvalue =

f(1) = 1.200 fvalue = f(2) = 2.200 fvalue = f(3) = 3.900

Likewise, if we wanted to request input, we could replace the fvalue line in that script with e.g.

f(i) = input(sprintf('Type the value of f(%d): ',i));

to see

Type the value of f(1): 4.6 Type the value of f(2): 4.2 Type the value of f(3): 3.8

where we typed the numbers 4.6 ...

The printf function works exactly like sprintf, except in that it does not return a value to be assigned to a variable - it just prints the result to the screen.

Collections of characters such as these are called "character strings", or "strings" for short. Matlab has the usual complement of basic tools for manipulating these. A few examples of such functions are found in the table below.

Format |
Purpose |
Example |

strcmp(string1,string2) | Evaluates to "true" if string1 and string2 are the same. | if strcmp(myinput,'yes'), loop actions... |

strcat(string1,string2) | Concatenates string2 onto the end of string1. We can assign the result to a variable. | strcat('my ','goodness!') ans = my goodness! |

strfind(string1,string2) | Finds the location of the first occurence of string2 in string1 | strfind('This is junk','junk') ans = 9 |

The "final exam" for this course will take place
at 8:00 AM on Tuesday, 12 December. This will be an ordinary
50 minute test. It will be comprehensive, but weighted toward
the latter half of the semester. As always, paper notes will
be permitted, but no electronic devices will be allowed.

A
Solution example is available
for the quiz.

Assignment A is posted.