1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Matlab User-defined function problem

  1. Apr 11, 2015 #1
    1. The problem statement, all variables and given/known data
    Write a user-defined function that calculates grade point average (GPA) on a scale of 0 to 4 ...
    https://41.media.tumblr.com/4f5e8ee38181d7c584c234138f632714/tumblr_nmnhq9p8jo1usetlso1_1280.png [Broken]

    3. The attempt at a solution

    I'm getting the error that "function definitions are not permitted in this context"
    And details says "A nested function cannot be defined in a control statement"

    I don't really understand how to use functions. What can I do to fix this?

    Oh and I took out that extra "end"
    Last edited by a moderator: May 7, 2017
  2. jcsd
  3. Apr 11, 2015 #2


    Staff: Mentor

  4. Apr 12, 2015 #3


    User Avatar
    Gold Member

    2 things.

    1. in matlab you need to make functions as separate m files. so you will have one file for your function, then one file where you use it.

    2. you generally don't want to write to an input to the function. you are redefining g as you use it. You might want to use an intermediate variable to write to

    try g_letter and g_number
  5. Apr 13, 2015 #4
    Thank y'all! I figured out how to make the function work, but not with the Letter-to-number conversion I was trying to use. It didn't like the size of g when it was made of stings. So I just wrote g with the numbers instead of letter grades. Good enough!

    https://41.media.tumblr.com/b768db26c487e07c370262dc345cb3e2/tumblr_nmrwnl6bpg1usetlso2_250.png [Broken]
    https://41.media.tumblr.com/ce387187354b1499a74fa028c76f50a4/tumblr_nmrwnl6bpg1usetlso1_1280.png [Broken]
    Last edited by a moderator: May 7, 2017
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted