Function error | Error: Function definitions are not permitted at the prompt or in scripts.

3 ビュー (過去 30 日間)
Gaia
Gaia 2012 年 6 月 18 日
Hello everybody,
I'm a new member of this community. I'd appreciate your help for a problem that I'm having with a little, silly function whom I type the code here.
function [val_min, pos_min] = comparison1(Isole_mat,Elhierro_mat)
[r,c]=size(Isole_mat);
for i = 1:r
latP = Isole_mat(i,2);
lonP = Isole_mat(i,3);
diffLat = latP - Elhierro_mat(:,2);
diffLon = lonP - Elhierro_mat(:,3);
distanza(:,i) = diffLat.^2+diffLon.^2;
%[dmin, I] = min(distanza(:));
end
[val_min, pos_min] = min(distanza);
What's wrong with it? Why do I get always the error message | Error: Function definitions are not permitted at the prompt or in scripts.
I saved the function in a m.file within the same name...
Please, help me understand.
Thank you in advance!
  4 件のコメント
Walter Roberson
Walter Roberson 2012 年 6 月 19 日
Please check that in your comparison1.m file, that the only lines before the "function" line, are comment lines and blank lines.
Gaia
Gaia 2012 年 6 月 19 日
Yes they are.

サインインしてコメントする。

回答 (2 件)

per isakson
per isakson 2012 年 6 月 19 日
Do you have more than one m-file with the name, comparison1? Check with
which comparison1 -all
Is
function [val_min, pos_min] = comparison1(Isole_mat,Elhierro_mat)
.
the first line in the m-file? And is
end
.
the last line of the m-file? The "end", which match "function" is optional, but I think it makes the function easier to read.
The Code Analyzer box (or mlint-box) at the right top of the editor is it green or at least not red?
  1 件のコメント
Gaia
Gaia 2012 年 6 月 19 日
I have just one file named comparison1.
In the first line I have some comments lines and blank lines, and in the last lines there are the outputs values:
[val_min, pos_min] = min(distanza);
The box is orange, but this was a problem I already had; anyway it is due to the letter "c" in this statement:
[r,c]=size(Isole_mat);
because "the value assigned might never be used", and to the following line:
distanza(:,i) = diffLat.^2+diffLon.^2;
because "it might be growing inside a loop. Consider preallocating for speed."

サインインしてコメントする。


Gaia
Gaia 2012 年 6 月 19 日
A silly silly mistake... I was trying to call the function in the wrong way, forgetting that the variables aren't general but local...
Thank you all for your kind help!

カテゴリ

Help Center および File ExchangeArgument Definitions についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by