How can I solve this error?
2 views (last 30 days)
Show older comments
Pul on 11 Mar 2022
Commented: Rik on 14 Mar 2022
I'm not able to plot this because I get an error without explanation.
Can anyone help me, please?
% 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");
% plot the median values and 1-sigma confidence intervals
p1=plot(lakeData.Year,calibratedData.T(:,2)); hold on;
legend([p1 p2 p3],'Median','Lower 1-sigma','Upper 1-sigma');
ylabel('MAT above zero');
Jan on 11 Mar 2022
Edited: Jan on 11 Mar 2022
Omit the lines:
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.
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.
Find more on Loops and Conditional Statements 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!