This URL is http://www.stewart.cs.sdsu.edu/cs205/module8/index.html
We want to explore MATLAB Strings, which are arrays of characters. We'll need this for Wednesday's exploration of graphics and how to construct titles and labels.
First, let example a MATLAB command you've used - input.
and enter a number, say 45. Then repeat the command
We want to look at putting strings into aggregate structures. First, just concatenating strings:
What is we wanted to list the two words in separate rows of the matrix. Try
Notice the NASTY error message you get for s2. This is because you oare storing this data in a matrix, when you use more than one row, and you know that you must have the same number of columns for each row. It's time for another
We can have a padded character array
Or a cell array, which uses the { notation instead of the [ notation for matrices.
You can convert between padded character arrays and cell arrays
Let's have some fun with strings
rad = 2.5; area=pi*rad^2; t = ['A circle of radius ' num2str(rad) ' has an area of ' num2str(area) '.']; disp(t)Type the following command and then use your arrow keys to move back to the typed command and change one character to perform the following format investigations
fprintf('%.0e\n',pi)
fprintf('%.1e\n',pi)
fprintf('%.3e\n',pi)
fprintf('%.5e\n',pi)
fprintf('%.10e\n',pi)
fprintf('%.0f\n',pi)
fprintf('%.1f\n',pi)
fprintf('%.3f\n',pi)
fprintf('%.5f\n',pi)
fprintf('%.10f\n',pi)
fprintf('%.12f\n',pi)
fprintf('%.0g\n',pi)
fprintf('%.1g\n',pi)
fprintf('%.3g\n',pi)
fprintf('%.5g\n',pi)
fprintf('%.10g\n',pi)
fprintf('%8.0g\n',pi)
fprintf('%8.1g\n',pi)
fprintf('%8.3g\n',pi)
fprintf('%8.5g\n',pi)
fprintf('%8.10g\n',pi)
An even simple technique is to put all the information we've used
today together (along with the num2str command and have a
loop do the drudgery for us.
for i=1:5 fprintf(['%8.' int2str(i) 'g\n'],pi) end