Matlab User-defined function problem

  • Thread starter Thread starter BodenD
  • Start date Start date
  • Tags Tags
    Function Matlab
Click For Summary

Discussion Overview

The discussion revolves around a homework problem involving the creation of a user-defined function in MATLAB to calculate grade point average (GPA) on a scale of 0 to 4. Participants explore issues related to function definitions, file organization, and variable handling within MATLAB.

Discussion Character

  • Homework-related
  • Technical explanation

Main Points Raised

  • One participant reports an error stating "function definitions are not permitted in this context" and seeks clarification on how to properly use functions in MATLAB.
  • Another participant suggests that functions should be placed in separate m-files and provides a link to MATLAB documentation for creating functions.
  • A third participant emphasizes the importance of not overwriting input variables within the function and recommends using intermediate variables instead.
  • A later reply indicates that the original poster resolved their issue by simplifying their approach, opting to use numeric grades instead of letter grades, due to complications with string handling.

Areas of Agreement / Disagreement

Participants generally agree on the need for proper file organization when defining functions in MATLAB, but there is no consensus on the best approach to handle letter-to-number conversions within the function.

Contextual Notes

Some limitations include potential misunderstandings about MATLAB's function definition rules and variable handling, as well as the specific requirements for the GPA calculation that may not have been fully articulated.

BodenD
Messages
2
Reaction score
0

Homework Statement


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

The Attempt at a Solution


tumblr_nmnhq9p8jo1usetlso2_1280.png


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:
Physics news on Phys.org
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
 
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
https://41.media.tumblr.com/ce387187354b1499a74fa028c76f50a4/tumblr_nmrwnl6bpg1usetlso1_1280.png
 
Last edited by a moderator:

Similar threads

  • · Replies 8 ·
Replies
8
Views
9K
  • · Replies 7 ·
Replies
7
Views
3K
Replies
1
Views
2K
Replies
2
Views
10K
  • · Replies 19 ·
Replies
19
Views
2K
  • · Replies 1 ·
Replies
1
Views
20K
  • · Replies 26 ·
Replies
26
Views
3K
  • · Replies 12 ·
Replies
12
Views
4K
Replies
6
Views
30K
  • · Replies 3 ·
Replies
3
Views
6K