Chapter 7: Exercise 32:
Write a function readthem that prompts the user for a string consisting of a
number immediately followed by a letter of the alphabet. The function error‐checks
to make sure that the first part of the string is actually a number, and to make sure
that the last character is actually a letter of the alphabet. The function returns the
number and letter as separate output arguments. Note: if a string ‘S’ is not a
number, str2num(S) returns the empty vector. An example of calling the function
follows:
Chapter 7: Exercise 35
Write the beautyofmath script described in Chapter 4, Problem 19, as a string
problem.
Chapter 8: Exercise 20
A script stores information on potential subjects for an experiment in a vector of
structures called subjects. The following show an example of what the contents
might be:
subjects
subjects =
1x3 struct array with fields:
name
sub_id
height
weight
>> subjects(1)
ans =
name: 'Joey'
sub_id: 111
For this particular experiment, the only subjects who are eligible are those whose
height or weight is lower than the average height or weight of all subjects. The
script will print the names of those who are eligible. Create a vector with sample
data in a script, and then write the code to accomplish this. Don’t assume that the
length of the vector is known; the code should be general.
Chapter 8: Exercise 21
Quiz data for a class is stored in a file. Each line in the file has the student ID
number (which is an integer) followed by the quiz scores for that student. For
example, if there are four students and three quizzes for each, the file might look like
this:
44 7 7.5 8
33 5.5 6 6.5
37 8 8 8
24 6 7 8
First create the data file, and then store the data in a script in a vector of structures.
Each element in the vector will be a structure that has 2 members: the integer
student ID number, and a vector of quiz scores. The structure will look like this:
To accomplish this, first use the load function to read all information from the file
into a matrix. Then, using nested loops, copy the data into a vector of structures as
specified. Then, the script will calculate and print the quiz average for each student.
For example, for the previous file:
Chapter 9: Exercise 1
Write a script that will read from a file x and y data points in the following
format:
x0y1
x 1.3 y 2.2
x 2.2 y 6
x 3.4 y 7.4
The format of every line in the file is the letter ‘x’, a space, the x value, space, the
letter ‘y’, space, and the y value. First, create the data file with 10 lines in this
format. Do this by using the Editor/Debugger, then File Save As xypts.dat. The
script will attempt to open the data file and error‐check to make sure it was opened.
If so, it uses a for loop and fgetl to read each line as a string. In the loop, it creates x
and y vectors for the data points. After the loop, it plots these points and attempts
to close the file. The script should print whether or not the file was successfully
closed.
Chapter 9: Exercise 24
A spreadsheet popdata.xls stores the population every 20 years for a small town
that underwent a boom and then decline. Create this spreadsheet (include the header
row) and then read the headers into a cell array and the numbers into a
matrix. Plot the data using the header strings on the axis labels.
Year Population
1920 4021
1940 8053
1960 14994
1980 9942
2000 3385
% Read population data from a spreadsheet
% and plot it
%Read numbers and text into separate variables
[num txt] = xlsread('popdata.xls');
%Create vectors for the plot
year = num(:,1);
pop = num(:,2);
plot(year,pop','ko')
%Axes labels based on header strings
xlabel(txt{1})
ylabel(txt{2})