please any help appreciated

I have an assignment to do in matlab:Write a Matlab program tha accepts a numerical value x fro 0 to 100 as input and computes and displays the corresponding letter grade given by the following table:

Letter Grade Marks
A x greater than or equal to 90
B x greater than or equal to 80 and x less than or equal to 89
C x greater than or equal to 70 and x less than or equal to 79
D x greater than or equal to 60 and x less than or equal to 69
F x less 60

Im not that experianced and so cant see what Ive done wrong but keep getting an error saying error==> line 8, which is the val=x line.

Does anyone see what I've done wrong.

This is my code:

function y=grade(x)
%This function accepts a numerical value, x, from 0 to 100
%and computes and displays the corresponding letter grade

%Enter the number as an input argument and a grade from A to F will be
%displayed which corresponds to the number entered.

switch val
case x>=90
case 80<=x<=89
case 70<=x<=79
case 60<=x<=69


  • I try your code and it gives me no errors. So, how do you call the function?

    Then there are some errors in your function:

    1. The function has no output so the first line has to be:
    function grade(x)

    2. I think you can't use this sintax "80<=x<=89" but you have to write "case x>=80 && x<=89"

    3. it is not necessary to use also the variable "var". you can use x to evaluate all the case expressions

    3. I think in this case it is not possible to use the switch in this way. If you try the code it gives you the wrong output. You have to put as switch expression "logical(true)", in this way:

    switch logical(true)
    case ...
  • ah i see. thanks. two more things though. If i used if and elseif statemnets instead of switch like:
    if (g>=90)
    elseif (80<=g<=89)
    elseif (70<=g<=79)
    elseif (60<=g<=69)

    why do i get an error for g>=90. I understand that the 80<=g<=89 should be as you said before.
    And secondly the prevoius program you showed me with the logicla(true) part works great howver if i don't put an input argument why does it say there is an error in line 10??
  • It is difficult to me to understand what causes the error if you don't specify what kind of error is and how you call the function. I try the code and it runs without errors...

    The if-elseif statement is ok.

    I can't understand what do you mean with "if i don't put an input argument".
  • i only put in "grade" instead of "grade(45)" for example, and it displays Error in ==> score at 9
    if (g>=90).

    does this mean that there is a bug in my code or is it purely because i didnt put an input argument such as 45 into grade(x).?
    if i had two output arguments in a m-file program could i somehow display them as a vector??

    thanks so much for you help
  • It is not a bug of your code. Your function want an input and if you don't put the input it gives an error. It's the same if you use a matlab function, i.e. [u]sin[/u]. It expects an argument, and if you put "sin" and not "sin(x)" it gives an error.

    You can display the output as a vector. You have to put the output in the first line of the function, i.e.:

    function y=fun_name(z)
    y=[z+1 z+2 z+3];

    And when you call the function you have to call it in this way:


    in this way the output y is a vector [2 3 4];
    But it could be also a scalar or a matrix, it depends on what is y inside your function.
  • thanks alot!!! now ive got both programs i needed to get done finished. appreciate it!!
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


In this Discussion