their something wrong with my script need help please

조회 수: 16 (최근 30일)
Mutaz Alsomali
Mutaz Alsomali 2018년 7월 5일
댓글: Rik 2020년 5월 13일
%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
Guillaume
Guillaume 2018년 7월 5일
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 2018년 7월 5일
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.

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

채택된 답변

Jan
Jan 2018년 7월 5일
편집: Jan 2018년 7월 5일
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
Walter Roberson
Walter Roberson 2020년 5월 13일
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 2020년 5월 13일
편집: Walter Roberson 2020년 5월 13일
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.

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

추가 답변 (1개)

Andrey Porokhnyuk
Andrey Porokhnyuk 2020년 1월 21일
편집: Andrey Porokhnyuk 2020년 1월 21일
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
Guillaume
Guillaume 2020년 1월 21일
편집: Guillaume 2020년 1월 21일
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 2020년 5월 13일
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.

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

카테고리

Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by