Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(MatLab) Problem 1 In this problem, write the first of two functions. This first function takes in the first and last name and returns a

(MatLab)

Problem 1

In this problem, write the first of two functions. This first function takes in the first and last name and returns a string that is lastname_firstname and with any dashes (-) in the name taken out.

Note that youll be testing it by calling it from the command line; youll call it from a script in problem 3.

Deliverables:

Function file that takes in a string and returns a string without a dash

Call that function from the command line and show that it works

strNoDash = RemoveDash(first-dash);

Function file that takes in first and last name and returns the joined name

Call the function from the command line and show that it works

str = JoinName(first, last)

str = JoinName(first, last-dash);

str = JoinName(first-dash, last-dash);

Step by Step Instructions:

Create a function file. It should take in two variables and return one.

Start with using strcat or sprintf to join the names together. Check that it works with first and last (call from the command line)

Note: One trick is to copy the first line of the function file, delete the function key word, and then put your own variable values in. This makes sure that you have the inputs and the outputs in the right order

I.e., copy function function [strOut] = JoinNames( strFirst, strLast ) to the command line,

and then edit to be

strOut = JoinNames(first, last)

Now add code to your function to check for the dash (-) in either the first or last name

Try strfind(first). What do you get?

try strfind(first-dash). What do you get?

Youll need isempty to check for an empty return

If you have a dash, you need to delete it. Theres two ways to do this. Either set that element to the empty array

array(k) = [] - deletes the kth element from array

array = strcat( array(1:k-1), array(k+1:end) ) set the array to the first and second halves

Make another function that takes in a string and returns the same string with no dashes. Call this on both inputs from within your join string function

Self-check:

>> strOut = RemoveDash('first-dash')

strOut =

firstdash

>> str = JoinName('first', 'last')

str =

last_first

>> str = JoinName('first-dash', 'last')

str =

last_firstdash

>> str = JoinName('first-dash', 'last-dash')

str =

lastdash_firstdash

>>

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, well 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 As and Cs. You must do this by looking at the difference, not just a huge number of if-else statements.

Deliverables:

Function file that takes in numeric grade and returns a string

Call the function from the command line

str = ToLetterGrade(95)

str = ToLetterGrade(85)

str = ToLetterGrade(75)

Extra credit

str = LetterGrade( 98)

str = LetterGrade( 82)

Step by Step Instructions:

Create a function file that takes in one input and returns one output

There are a variety of ways to set up the if statement to create a letter grade output. Id suggest the following

Check if the grade is larger than 90 -> thats an A

Check if the grade is larger than 80 -> thats a B

Otherwise, a C

Extra credit: The point is to do this smartly, not just with 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.

Calculate the left over eg, grade 80 for a B grade

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 =

A

>> str = LetterGrade(85)

str =

B

>> str = LetterGrade(75)

str =

C

>>

EXTRA CREDIT:

>> str = LetterGrade( 98)

str =

A+

>> str = LetterGrade( 82)

str =

B-

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:

Script to read in data and write out data

Output file

Step by Step Instructions:

Use ReadData.m to read in the data. This function returns a cell array and a numeric array.

Check that the data was read correctly by printing out the first name and grade value. Use the command line for this.

[dT dN] = ReadData(ClassData.csv)

Read in the data in your script

Open up a file to write to using fopen

Write out headers for your new data file (column1Name, column2Name )

fprintf(fid, string);

Loop over all of the names remember that the first row is a header row, so skip it

Use the functions you created to convert the data and create the two output strings

Use fprintf to print out your new strings to the file (dont forget the comma and the )

Close the file using fclose.

Problem 4- Extra Credit

Plot the function A*sin(x) + 2(x2) (x = 0 to 5) for 5 values of A. Then, create a legend with string functions. To get the extra credit, an anonymous function and for loop must be used.

Step by Step Instructions:

The goal of the problem will be to create individual strings for each needed legend entry and store the entries into a cell array, before finally calling legend.

To start, define A = [1,3,5,7,9]

Define your function anonymously (Depending on where you place it in your code, youll need to make your function have 1 or 2 local variables (either @(x) or @(a,x)))

Plot the function in a for loop while varying the appropriate variable

Define the legend entry for the loop by stating, as a string, what A equals for the line being plotted. Store into a cell array

Consider how the previous point should be done. What can be kept the same? What needs to be different? Look up (if you dont already understand) commands like strcat or sprint. The command num2str should also help

After the for loop, call the legend command with the cell array of legend entries. Move the legend so it doesnt sit on the graph

Class Data :

First Last Grade
Bunny Summer 67
Flying Dutchman 83
Flower de-Derain 92
Tyvec Ling 87

ReadData :

function [ dataT, dataN ] = ReadData( strFName ) %UNTITLED2 Summary of this function goes here % Detailed explanation goes here

fid = fopen( strFName, 'r' ); if fid == -1 fprintf('Bad file %s ', fid); end

nCols = length( FindCommas( fid ) ); nRows = 1; while ~feof(fid) nCols = [nCols length(FindCommas(fid))]; nRows = nRows + 1; end

fseek(fid, 0, 'bof');

dataT = cell(nRows, max(nCols)); dataN = zeros(nRows, max(nCols)) - 1; for r = 1:nRows [nCommas, str] = FindCommas(fid); nCommas = [ 0 nCommas ]; for c = 1:nCols(r) strItem = str(nCommas(c)+1:nCommas(c+1)-1); nItem = str2num( strItem ); if ~isempty(nItem) && size(nItem,1) == 1 && size(nItem,2) == 1 && isnumeric(nItem) dataN(r,c) = nItem; else dataT{r,c} = strItem; end end

end fclose(fid); end

FindCommas:

function [ commas, strAll ] = FindCommas( fid ) %FindCommas Read a csv file line with commas as deliminators % Deals with commas in "" correctly

commas = []; bInQuote = false;

str = fgets(fid); pos = 1; strAll = str; while bInQuote == true || length(commas) == 0 bFound = false; for k = 1:length(str) if bInQuote if str(k) == '"' bInQuote = false; bFound = true; end elseif str(k) == '"' bInQuote = true; bFound = true; elseif str(k) == ',' commas = [commas pos]; bFound = true; end pos = pos + 1; end if bInQuote str = fgets(fid); strAll = [strAll, str]; end if bFound == false fprintf('Looping ') end end

commas = [commas, length(str)]; end

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Essentials of Database Management

Authors: Jeffrey A. Hoffer, Heikki Topi, Ramesh Venkataraman

1st edition

133405680, 9780133547702 , 978-0133405682

More Books

Students also viewed these Databases questions

Question

Over what timescale should the project be undertaken?

Answered: 1 week ago