their something wrong with my script need help please

5 views (last 30 days)
%the dewpoint function
function Td= dewpoint(T, RH)
%set the a and b values
a = 17.27;
b = 237.7;
%declare the
Td=[];
f=[];
for i=1:length(RH)
%compute f
f(i) = a.*T./(b + T) + log(RH(i));
%copute Td
Td(i) = b.*f(i)./(a-f(i));
end
end
%the my_plot function
function p=my_plots(Td, RH)
%plot and labeling
plot(Td, RH)
title('Mutaz alsomali Dew point plot')
xlabel('Td (F)')
ylabel('RH(%)')
end
%test case1
T=15
RH=0.4
Td= dewpoint(T, RH)
%test case 2
T=35
RH=0.8
Td= dewpoint(T, RH)
figure
%plot for 25
T=25;
RH=0.1:0.05:0.95;
Td= dewpoint(T, RH);
my_plots(Td, RH);
hold on
%plot for 32F
T=(32-32).*5./9;
Td= dewpoint(T, RH);
my_plots(Td, RH);
hold on
%plot for 77F
T=(77-32).*5./9;
Td= dewpoint(T, RH);
my_plots(Td, RH);
hold on
%plot for 95F
T=(95-32).*5./9;
Td= dewpoint(T, RH);
my_plots(Td, RH);
legend('25 C', '32F', '77F', '95F')
hold off
  2 Comments
Guillaume
Guillaume on 5 Jul 2018
You should tell us what is wrong with your script rather than letting us guess. If you get an error, then give us the full text of the error message (everything in red). If you don't get the result you expected then what did you expect and what did you get instead?
Mutaz Alsomali
Mutaz Alsomali on 5 Jul 2018
Error: File: dewpoint.m Line: 29 Column: 1 Function definitions in a script must appear at the end of the file. Move all statements after the "dewpoint" function definition to before the first local function definition.

Sign in to comment.

Accepted Answer

Jan
Jan on 5 Jul 2018
Edited: Jan on 5 Jul 2018
The error message is clear already and contains the required instructions:
Function definitions in a script must appear at the end of the file.
Move all statements after the "dewpoint" function definition to before
the first local function definition.
Did you try this already?
%test case1
T=15
RH=0.4
Td= dewpoint(T, RH)
%test case 2
T=35
RH=0.8
Td= dewpoint(T, RH)
figure
%plot for 25
T=25;
RH=0.1:0.05:0.95;
Td= dewpoint(T, RH);
my_plots(Td, RH);
hold on
%plot for 32F
T=(32-32).*5./9;
Td= dewpoint(T, RH);
my_plots(Td, RH);
hold on
%plot for 77F
T=(77-32).*5./9;
Td= dewpoint(T, RH);
my_plots(Td, RH);
hold on
%plot for 95F
T=(95-32).*5./9;
Td= dewpoint(T, RH);
my_plots(Td, RH);
legend('25 C', '32F', '77F', '95F')
hold off
function Td = dewpoint(T, RH)
%set the a and b values
a = 17.27;
b = 237.7;
%declare the
Td= zeros(1, length(RHS)); % PRE-ALLOCATE !!!
f=zeros(1, length(RHS)); % PRE-ALLOCATE !!!
for i=1:length(RH)
%compute f
f(i) = a.*T./(b + T) + log(RH(i));
%copute Td
Td(i) = b.*f(i)./(a-f(i));
end
end
%the my_plot function
function p=my_plots(Td, RH)
%plot and labeling
plot(Td, RH)
title('Mutaz alsomali Dew point plot')
xlabel('Td (F)')
ylabel('RH(%)')
end
I've simple moved the script part of the file to the top as explained in the message.
By the way: Try to write meaningful comments. "%set the a and b values" is very obvious, but where do the values come from? "%declare the" is not useful and "%the my_plot function" is neither. Comments will save your live as programmer. While an exhaustively commented code can be expanded, maintained and debugged, missing comments leaves code in a status, in which deleting and rewriting is the most reliable option.
Pre-allocation is much more efficient than letting an array grow iteratively.
  6 Comments
Walter Roberson
Walter Roberson on 13 May 2020
I thought the script m file had to be same as the fuction name
No, the script name must be different than the name of any (non-nested) function you define inside it.
Walter Roberson
Walter Roberson on 13 May 2020
Edited: Walter Roberson on 13 May 2020
Look up at the top of this page, to the left top immediately underneath the title. Click on Follow.
Now visit
https://www.mathworks.com/matlabcentral/profile/authors/16345297-davood-ostadalipoor/notification_preferences and you should be able to configure receiving email when there are updates to content you are following.
You can see recent activity on material you are following by looking at https://www.mathworks.com/matlabcentral/profile/authors/16345297/activities
Note: these links apply only to you, and should get you directly to the features you are looking for. There are menu entries that can lead you there without having to know the magic URLs.

Sign in to comment.

More Answers (1)

Andrey Porokhnyuk
Andrey Porokhnyuk on 21 Jan 2020
Edited: Andrey Porokhnyuk on 21 Jan 2020
I am moving from Octave, and these restrictions in Matlab are very confusing.
Simply, I had multiple functions in a complex script. When I moved all of them to the end, the interpretter was still throwing this senseless error.
The below-like script throws the same error:
"Error: File: script.m Line: 13 Column: 4
Function definitions in a script must appear at the end of the file.
Move all statements after the "fund" function definition to before the first local function definition.
%% the script code
a=[];
b=[];
c=fund(a,b);
a=fune(a,c);
%functions below
func = @(x, varargin) x(varargin{:});
function a=fund(b,c)
%some code
end;
function a=fune(b,c)
%some code
end;
So, what do you think?
";"
Semicolon!
Matlab has got indigestion from simple semicolon, indicating the silent end of instruction.
No, really, why is it so picky? It is not C++, where {curvies} contain the definition. There are "starting word", and "finishing word" for one declaration/definition. Since it looks like any other instruction, why doesn't it accept semicolon?
  2 Comments
Guillaume
Guillaume on 21 Jan 2020
Edited: Guillaume on 21 Jan 2020
You would be much better off reposting this as a new question. At the very least, it should have been posted as a comment to the question. It's certainly not an answer.
Rik
Rik on 13 May 2020
A more pertinent question would also be why a complex script isn't a function in the first place. Using functions in scripts should in my view be considered a rapid prototyping tool or a debugging feature.
Also, mlint will immediatly point you to the location that is the source of the error. Tools like mlint are the reason I personally prefer to program in Matlab and only after that test compatibility in Octave.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!