their something wrong with my script need help please

%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

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?
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

i did that before and it showed me this: "Error: File: dewpoint.m Line: 38 Column: 15 Function with duplicate name "dewpoint" cannot be defined." so a had to made the arrangement that i put in display and it still make an error for some reason.
thank you for your comment and ill keep your advice in mind
It is safer to work with functions than with scripts. Scripts can modify variables in workspace of the caller, which can be confusing also.
I'm happy that I could help you.
Hi, I tried to run the above code mentioned in "Did you try this" box above and got the following error which is exactly the same error message when I run my function code from command line. I thought the script m file had to be same as the fuction name as I had similar problems which were resoled by changing the file name to the same name as function name in the script?
Here is the error message please:
Error: File: dewpoint.m Line: 32 Column: 15
Local function name must be different from the script name.
Also where and how can I find out any responses and answers left by anyone to my questions on Matlab, is it possible to get an email alert please?
Thank you
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.
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)

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

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.
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.

Categories

Find more on Programming in Help Center and File Exchange

Asked:

on 5 Jul 2018

Commented:

Rik
on 13 May 2020

Community Treasure Hunt

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

Start Hunting!