How can I solve this error?

조회 수: 1 (최근 30일)
Pul
Pul 2022년 3월 11일
댓글: Rik 2022년 3월 14일
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
Jan
Jan 2022년 3월 11일
In the file you have posted, there is no "end" in the last line of the file baymbt_predict.m .
Pul
Pul 2022년 3월 14일
@Simon Chan Thank you Simon, but also in that way, it doesn't work.

댓글을 달려면 로그인하십시오.

답변 (1개)

Jan
Jan 2022년 3월 11일
편집: Jan 2022년 3월 11일
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
Pul
Pul 2022년 3월 14일
@Rik Yes, thank you for the suggestion.
But that script doesn't work anyway.
Rik
Rik 2022년 3월 14일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by