error using function code

I have an assignment due soon and i can not get matlab to work. I have never used matlab before and this is my first asignment in it. I have to make a program where i input a score fom 0 to 100 and it tells me what the grade is.
the first line of code needs to be
function grade = lettergrade(score)
but every time i try this, i get a error and matlab will not let me proced. I have even tried to copy someones code and it fails. I even copied the example from my book by copy paste and it throws the same error
error: function definition not supported in this context. functions can only be created as local or nested functions
no idea what the heck this even means

Respuestas (2)

James Tursa
James Tursa el 16 de Mayo de 2022
Editada: James Tursa el 17 de Mayo de 2022

0 votos

Create a file called lettergrade.m somewhere on the MATLAB path (e.g., in your working directory)
edit lettergrade.m
Copy all of your lettergrade function code into that file and save it.
Then simply call the lettergrade function like you would call any other function. From the command line or from within another file.
The reason what you are currently doing isn't working is because MATLAB thinks you are trying to create a function on the fly at the command line.
Note that if you were trying to create a function called "grade" as your example code shows, you would create a file called grade.m rather than lettergrade.m (but your instructions are to create a function called lettergrade)

4 comentarios

Matt Thomas
Matt Thomas el 17 de Mayo de 2022
how do i create an m file and save it? i have no idea how to use matlab haha
James Tursa
James Tursa el 17 de Mayo de 2022
Editada: James Tursa el 17 de Mayo de 2022
I showed you exactly what to type to create the file. Simply type this at the command line:
edit lettergrade.m
You can copy and paste text code into the file using the usual Ctrl-c and Ctrl-v stuff. To save it simply click on the "disk" icon as you would in any other application.
Matt Thomas
Matt Thomas el 17 de Mayo de 2022
ohhh, i didnt know the "edit lettergrade.m" created the m file. I thought it edited the m file i needed to create first haha.
ok, i will try this. thanks
James Tursa
James Tursa el 17 de Mayo de 2022
It does both. If the file already exists then it will edit the file. If the file doesn't exist then it will create it.

Iniciar sesión para comentar.

Voss
Voss el 17 de Mayo de 2022

0 votos

This error means you cannot define a function on the MATLAB command-line. You need to create an m-file and define the function in there.
For example, if your function should be called lettergrade, you can enter
edit lettergrade
on the command-line (and say yes if MATLAB asks you if you want to create a new m-file).
Then put the code in there, with the first line being:
function grade = lettergrade(score)
After that's done, then you can run the function from the command-line or from other functions (or from scripts, which are also m-files) by doing, say,
my_grade = lettergrade(84);
which should store the value 'B' in the variable my_grade, since an 84 should be a B.

4 comentarios

Matt Thomas
Matt Thomas el 17 de Mayo de 2022
ok, that makes sense now. i have never programmed or used matlab and this class is brutal haha.
thanks for the help
Steven Lord
Steven Lord el 17 de Mayo de 2022
Since you've never used MATLAB before I suggest you start with the free 2 hour MATLAB Onramp tutorial (https://www.mathworks.com/support/learn-with-matlab-tutorials.html) to quickly learn the essentials of MATLAB.
Matt Thomas
Matt Thomas el 17 de Mayo de 2022
i can do that. thanks for showing me that. Does it cover things like if, elseif, for loops etc?
Cris LaPierre
Cris LaPierre el 17 de Mayo de 2022
See this page on creating functions in file:
Ch 12 of MATLAB Onramp coversthe basics of if statements and for loops.

Iniciar sesión para comentar.

Categorías

Más información sobre Desktop en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 16 de Mayo de 2022

Comentada:

el 17 de Mayo de 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by