How can I solve this error?

1 view (last 30 days)
Pul on 11 Mar 2022
Commented: Rik on 14 Mar 2022
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
%upper 1-sigma
legend([p1 p2 p3],'Median','Lower 1-sigma','Upper 1-sigma');
ylabel('MAT above zero');
Jan on 11 Mar 2022
In the file you have posted, there is no "end" in the last line of the file baymbt_predict.m .
Pul on 14 Mar 2022
@Simon Chan Thank you Simon, but also in that way, it doesn't work.

Sign in to comment.

Answers (1)

Jan on 11 Mar 2022
Edited: Jan on 11 Mar 2022
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)
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:
Read the documentation to learn the differences between scripts and functions.
Pul on 14 Mar 2022
@Rik Yes, thank you for the suggestion.
But that script doesn't work anyway.
Rik on 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.

Sign in to comment.


Find more on Multidimensional Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!