How can I solve this error?

Hello everyone,
I'm not able to plot this because I get an error without explanation.
Can anyone help me, please?
Thank you!
% read in data
lakeData = readtable('test_C.csv');
% pull out mbt5me for ease
mbt5me = lakeData.MBT5Me;
% set prior mean, standard deviation, and model type
prior_mean = 10;
prior_std = 10;
model = "T0";
% calibrate both uncorrected mbt and corrected mbt:
calibratedData = baymbt_predict(mbt5me,prior_mean,prior_std,model,"lake");
Error: File: /users/mss.system.imXuj1/baymbt_predict.m Line: 53 Column: 5
Function definitions in a script must appear at the end of the file.
Move all statements after the "baymbt_predict" function definition to before the first local function definition.
% plot the median values and 1-sigma confidence intervals
figure(1); clf;
p1=plot(lakeData.Year,calibratedData.T(:,2)); hold on;
%lower 1-sigma
p2=plot(lakeData.Year,calibratedData.T(:,2)-(calibratedData.T(:,2)-calibratedData.T(:,1))/2);
%upper 1-sigma
p3=plot(lakeData.Year,calibratedData.T(:,2)+(calibratedData.T(:,3)-calibratedData.T(:,2))/2);
legend([p1 p2 p3],'Median','Lower 1-sigma','Upper 1-sigma');
xlabel('Year');
ylabel('MAT above zero');

6 commentaires

Rik
Rik le 11 Mar 2022
I just ran your code in the online environment. The error message seems clear enough to me.
Why don't you undo the edits you made to baymbt_predict.m? clear all, close all should not exist in code like this.
Pul
Pul le 11 Mar 2022
Modifié(e) : Pul le 11 Mar 2022
@Rik thanks.
(I only get this error when I run it: Error in lakes (line 29)
calibratedData = baymbt_predict(mbt5me,prior_mean,prior_std,model,"lake");)
What kind of edits in 1baymbt_predict.m do you mean?
Anyways, I don't know how to solve this error.
Simon Chan
Simon Chan le 11 Mar 2022
As mentioned by Rik, remove those 2 lines and move the 'end' which terminate the function to the last line of the code.
Attached for your reference
Pul
Pul le 11 Mar 2022
@Simon Chan I'm sorry Simon, but I don't understand: "end" is already the last line of the code.
Could you explain it to me again, please? Thanks!
Jan
Jan le 11 Mar 2022
In the file you have posted, there is no "end" in the last line of the file baymbt_predict.m .
Pul
Pul le 14 Mar 2022
@Simon Chan Thank you Simon, but also in that way, it doesn't work.

Connectez-vous pour commenter.

Réponses (1)

Jan
Jan le 11 Mar 2022
Modifié(e) : Jan le 11 Mar 2022

0 votes

Omit the lines:
clear all
close all
Note, that clear all deletes all loaded functions from the RAM and reloading them from the slow disk wastes time only. This does not have any benefits.
Then read the error message: Scripts must be defined on top of the M-files. Functions used inside the script must be defined on bottom. Your file baymbt_predict.m is a script (because it does not start with the keyword "function"). It runs the two brute clearing commands. Then an empty function is created:
function output = baymbt_predict(mbt5me,prior_mean,prior_std,Tmodel,Type)
end
And in line 53 another script is started. Now the error message tells you, that this is not allowed, because you have a function defined before.
I guess, you want to remove the two "clear and close" lines, and move the "end" from line 7 to the bottom of the code. Then the file baymbt_predict.m becomes a function and calling it with inputs gets meaningful:
baymbt_predict(mbt5me,prior_mean,prior_std,model,"lake")
Read the documentation to learn the differences between scripts and functions.

7 commentaires

Pul
Pul le 11 Mar 2022
Thanks.
But if I do like you said, I get this error when I try to run this line: function output = baymbt_predict(mbt5me,prior_mean,prior_std,Tmodel,Type)
Error: Function definition are not supported in this context. Functions can only be created as local or nested functions in code files.
I deleted "clear all and close all" and I put end at the end of the script.
Rik
Rik le 11 Mar 2022
Attach your edited file.
Jan
Jan le 11 Mar 2022
Modifié(e) : Jan le 11 Mar 2022
I've attached a cleaned M-file.
Where does the "clear all, close all" come from? Did you insert it? Do you see, how I've moved the "end" from the line after "function" to the end of the code?
Did you download the software from here: https://github.com/jesstierney/BayMBT/blob/master/baymbt_predict.m ? Here "clear all, close all" and the "end" after "function" does not exist.
Pul
Pul le 13 Mar 2022
Thanks @Jan.
Yes, I donwloaded it from that website. Yes, I inserted it.
However, this is the error I get if I tried to run your code: >> function output = baymbt_predict(mbt5me,prior_mean,prior_std,Tmodel,Type)
function output = baymbt_predict(mbt5me,prior_mean,prior_std,Tmodel,Type)
Error: Function definition are not supported in this context. Functions can only be created as local or nested functions
in code files.
Rik
Rik le 14 Mar 2022
You should consider a basic Matlab tutorial. The Onramp tutorial is provided for free by Mathworks.
This is not the way to call a function, this is the way to define a function.
Pul
Pul le 14 Mar 2022
@Rik Yes, thank you for the suggestion.
But that script doesn't work anyway.
Rik
Rik le 14 Mar 2022
Which script? The file you posted was a function (that became a script after your edits, causing the initial problem). Now you are calling it with the incorrect syntax, causing another error.
This is really basic Matlab syntax.
Nobody was born knowing how this works. There is no shame in that. You should really do a basic toturial to learn the tool you're using. That way you can answer many questions yourself, and you will make it easier for us to help you fix future problems.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Graphics Performance dans Centre d'aide et File Exchange

Question posée :

Pul
le 11 Mar 2022

Commenté :

Rik
le 14 Mar 2022

Community Treasure Hunt

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

Start Hunting!

Translated by