ENGR112 Lab7 Strings and Files PDF
ENGR112 Lab7 Strings and Files PDF
Learning objectives:
• Create and manipulate strings
• Convert strings to and from numbers
• Reading and writing data files
Lab Problems
Files to download to your Lab7 Folder
• ClassData.csv
• ReadData.m
• FindCommas.m
Getting Started
Anything for experimentation goes here with bullet points
• In the command window do:
o nFound = strfind(‘hello-bye’, ‘-‘);
What does it return? Count characters
What does empty(nFound) return?
o nFound = strfind(‘hello-bye’, ‘ ‘);
o empty(nFound)
What does this return?
• Make sure that [dT, dN] = ReadData(‘ClassData.csv’) works (call it on the command line)
• Open up ClassData.csv in excel
• Match that to what dT and dN have in them
Week 7 ENGR 112 In lab exercise
Note: The overall goal is to convert ClassData.csv from its current format to one
that looks like lastname_firstname, letter grade (see problem 3). We’re going to
do this in pieces; Problem 1 and problem 2 create functions that get you set up to
do this in Problem 3.
Problem 1
In this problem, you will write two functions. The first function takes in a string
and returns that string without any dashes. The second function takes in the
first and last name and returns a string that is lastname_firstname and uses the
previous function to remove any dashes (-) in the name.
Note that you’ll be testing it by calling it from the command line; you’ll call it
from a script in problem 3.
Deliverables:
1. Function file that takes in a string and returns a string without a dash
a. A script is not necessary for this problem
2. Call that function from the command line and show that it works
a. strNoDash = RemoveDash(‘first-name);
i. For this problem, you will not need to create a script to show your
function works.
3. Function file that takes in first and last name and returns the joined name
a. This should include the RemoveDash function ran for both the first and last
name
4. Call the function from the command line and show that it works
a. str = JoinName(‘first’, ‘last’)
b. str = JoinName(‘first, ‘last-name);
c. str = JoinName(‘first-name, ‘last-name);
Create a function file that takes in a string and returns the same string with no dashes.
• Add code to your function to check for the dash (-) in either the first or last name
o Try strfind(‘first’). What do you get?
o try strfind(‘first-dash’). What do you get?
o You’ll need isempty to check for an empty return
• If you have a dash, you need to delete it. There’s two ways to do this. Either set that
element to the empty array
o array(k) = [] - deletes the kth element from array
Week 7 ENGR 112 In lab exercise
Self-check:
>> strOut = RemoveDash('first-dash')
strOut =
firstdash
str =
last_first
str =
last_firstdash
str =
lastdash_firstdash
>>
Grading Criteria:
[25 pts] [Function file for removing dashes]
[25 pts] [Function file for joining names]
Week 7 ENGR 112 In lab exercise
ans =
'CraigBurgess_Andy'
>>
Week 7 ENGR 112 In lab exercise
Problem 2
Write a function that takes in a numeric grade and returns a letter grade. 90+ is
an A, 80-90 is a B, anything below that is a C.
As before, we’ll test this function by calling it from the command window.
Extra credit: Add in +,- for > X7.5 and < X2.5. I.e., B+ is 88.5-90, B- is 80-82.5.
Make sure it works for A’s and C’s. You must do this by looking at the difference,
not just a huge number of if-else statements.
Deliverables:
5. Function file that takes in numeric grade and returns a string
a. Once again, you do not need to turn in a script
6. Call the function from the command line
a. str = ToLetterGrade(95)
b. str = ToLetterGrade(85)
c. str = ToLetterGrade(75)
7. Extra credit
a. str = LetterGrade( 98)
b. str = LetterGrade( 82)
Create a function file that takes in one input (numerical grade) and returns one output (letter
grade)
• There are a variety of ways to create the if statement to create a letter grade output. I’d
suggest the following
o Check if the grade is larger than 90 -> that’s an A
o Check if the grade is larger than 80 -> that’s a B
o Otherwise, a C
• Extra credit: The point is to do this without just using a huge number of if statements.
That way, if you were to add in additional letter grades (D and F) or change the
boundaries (to 0.3 and 0.7) the code would be easy to change.
o Calculate the “left over” – e.g. (grade – 80) for a B grade
o After you assign the main letter grade, add in the ‘+’ or ‘-‘ to the current letter,
based on the value of the left over. I.e., if the leftover were less than 0.25, you
would add a ‘-‘ to the ‘B’ string
Self-check:
>> str = LetterGrade(95)
str =
Week 7 ENGR 112 In lab exercise
str =
str =
>>
EXTRA CREDIT:
>> str = LetterGrade( 98)
str =
A+
str =
B-
Grading Criteria:
[40 pts] [Function file that takes in number and returns letter grade]
[20 pts ea] [Correctly assigns A,B,C]
[30 pts EC] Adds in +,-
Function scripts here:
function [gradeOut] = ToLetterGrade(gradeIn)
% Turn Percent Grade Into Letter Grade
gradeCompare = str2double(gradeIn);
if gradeCompare >= 90
gradeOut = 'A';
gradeCompare = gradeCompare - 90;
elseif gradeCompare >= 80 && gradeCompare < 90
gradeOut = 'B';
gradeCompare = gradeCompare - 80;
else
gradeOut = 'C';
gradeCompare = gradeCompare - 70;
end
end
>> ToLetterGrade('99');
>> ans
ans =
'A+'
>> ToLetterGrade('79');
>>
>> ans
ans =
'C+'
>> ToLetterGrade('71');
>> ans
ans =
'C-'
>>
Week 7 ENGR 112 In lab exercise
Problem 3
Read in the ClassData.csv file and write out another csv file that has
last_first,LetterGrade. Must use the functions created in problems 1 and 2.
Deliverables:
8. Script to read in data and write out data
9. Output file
Self-check:
Double click on the output filename in the matlab window. It will bring up a
data importer. Grab a screen shot:
Week 7 ENGR 112 In lab exercise
Or open up the file in any text editor/excel and copy and paste the data
Grading Criteria:
[10 pts] [Read in the data]
[10 pts] [Open up new file]
[10 pts] [Write out header]
[20 pts] [for loop over names – remember to use size of data here]
[20 pts] [Call functions to convert data]
[10 pts] [Write out new versions]
[10 pts] [Close file]
[10 pts] [Demonstrate it works]
Answer script here:
%%Andrew Burgess
%Lab 7 Problem 3
%Reset
clear;
clc;
Data = {'Name' 'Grade'};
%Read In
end
%Close File
fclose(fid);
Self-check:
Week 7 ENGR 112 In lab exercise
Grading Criteria:
[10 pts] [Define Anonymous function correctly (based on location within code)]
[10 pts] [Plotted correctly]
[60 pts] [Created legend string in for loop correctly]
[10 pts] [Called legend after loop]
[10 pts] [Moved legend to not cover graph]
Plot here:
Save your plot as a png and paste it here.