Dear All
I have developed the next code but I have a strange error. The error is (Error: File: dijkstraaaaa.m Line: 80 Column: 1 Function definitions are not permitted in this context.) I'll be grateful if you can help me
clear, clf format short
Vertices = 8;
Adj_Matrix=[ 0 20 0 0 0 0 15 0 ; ... 20 0 8 9 0 0 0 0 ; ... 0 8 0 6 15 0 0 10 ; ... 0 9 6 0 7 0 0 0 ; ... 0 0 15 7 0 22 18 0 ; ... 0 0 0 0 22 0 0 0 ; ... 15 0 0 0 18 0 0 0 ; ... 0 0 10 0 0 0 0 0 ];
State_Matrix=[ Inf 0 0 ; ... Inf 0 0 ; ... Inf 0 0 ; ... Inf 0 0 ; ... Inf 0 0 ; ... Inf 0 0 ; ... Inf 0 0 ; ... Inf 0 0 ];
Coord_Matrix=[ 0 5 ; 1 0 ; 5 1 ; 2 4 ; 3 6 ; 0 7 ; 4 8 ; 6 2 ];
text(Coord_Matrix(1,1), Coord_Matrix(1,2), '1'),hold on text(Coord_Matrix(2,1), Coord_Matrix(2,2), '2'),hold on text(Coord_Matrix(3,1), Coord_Matrix(3,2), '3'),hold on text(Coord_Matrix(4,1), Coord_Matrix(4,2), '4'),hold on text(Coord_Matrix(5,1), Coord_Matrix(5,2), '5'),hold on text(Coord_Matrix(6,1), Coord_Matrix(6,2), '6'),hold on text(Coord_Matrix(7,1), Coord_Matrix(7,2), '7'),hold on text(Coord_Matrix(8,1), Coord_Matrix(8,2), '8'),hold on
for i=1:Vertices for j=1:(i-1) if Adj_Matrix(i,j)~=0 Graphx=[Coord_Matrix(i,1) Coord_Matrix(j,1)]; Graphy=[Coord_Matrix(i,2) Coord_Matrix(j,2)]; plot(Graphx,Graphy,':'),hold on end; end; end;
axis('equal'); axis('off');
function [costs,paths] = dijkstra(AorV,xyCorE,SID,FID,iswaitbar)
error(nargchk(2,5,nargin)); all_positive = 1; [n,nc] = size(AorV); [m,mc] = size(xyCorE); [E,cost] = processInputs(AorV,xyCorE); if nargin < 5 iswaitbar = 0; end if nargin < 4 FID = (1:n); end if nargin < 3 SID = (1:n); end if max(SID) > n min(SID) < 1 eval(['help ' mfilename]); error('Invalid [SID] input. See help notes above.'); end if max(FID) > n min(FID) < 1 eval(['help ' mfilename]); error('Invalid [FID] input. See help notes above.'); end
isreversed = 0; if length(FID) < length(SID) E = E(:,[2 1]); cost = cost'; tmp = SID; SID = FID; FID = tmp; isreversed = 1; end
L = length(SID); M = length(FID); costs = zeros(L,M); paths = num2cell(nan(L,M));
if iswaitbar, wbh = waitbar(0,'Please Wait ... '); end for k = 1:L
if all_positive, TBL = sparse(1,n); else TBL = NaN(1,n); end
min_cost = Inf(1,n);
settled = zeros(1,n);
path = num2cell(nan(1,n));
I = SID(k);
min_cost(I) = 0;
TBL(I) = 0;
settled(I) = 1;
path(I) = {I};
while any(~settled(FID))
TAB = TBL;
if all_positive, TBL(I) = 0; else TBL(I) = NaN; end
nids = find(E(:,1) == I);
% Calculate the Costs to the Neighbor Points and Record Paths
for kk = 1:length(nids)
J = E(nids(kk),2);
if ~settled(J)
c = cost(I,J);
if all_positive, empty = ~TAB(J); else empty = isnan(TAB(J)); end
if empty || (TAB(J) > (TAB(I) + c))
TBL(J) = TAB(I) + c;
if isreversed
path{J} = [J path{I}];
else
path{J} = [path{I} J];
end
else
TBL(J) = TAB(J);
end
end
end
if all_positive, K = find(TBL); else K = find(~isnan(TBL)); end
N = find(TBL(K) == min(TBL(K)));
if isempty(N)
break
else
I = K(N(1));
min_cost(I) = TBL(I);
settled(I) = 1;
end
end
costs(k,:) = min_cost(FID);
paths(k,:) = path(FID);
if iswaitbar, waitbar(k/L,wbh); end
end
if iswaitbar, close(wbh); end
if isreversed costs = costs'; paths = paths'; end
if L == 1 && M == 1 paths = paths{1}; end end
[E,C] = processInputs(AorV,xyCorE)
C = sparse(n,n);
if n == nc
if m == n
if m == mc % Inputs: A,cost
A = AorV;
A = A - diag(diag(A));
C = xyCorE;
all_positive = all(C(logical(A)) > 0);
E = a2e(A);
else % Inputs: A,xy
A = AorV;
A = A - diag(diag(A));
xy = xyCorE;
E = a2e(A);
D = ve2d(xy,E);
all_positive = all(D > 0);
for row = 1:length(D)
C(E(row,1),E(row,2)) = D(row);
end
end
else
eval(['help ' mfilename]);
error('Invalid [A,xy] or [A,cost] inputs. See help notes above.');
end
else
if mc == 2 % Inputs: V,E
V = AorV;
E = xyCorE;
D = ve2d(V,E);
all_positive = all(D > 0);
for row = 1:m
C(E(row,1),E(row,2)) = D(row);
end
elseif mc == 3 % Inputs: V,E3
E3 = xyCorE;
all_positive = all(E3 > 0);
E = E3(:,1:2);
for row = 1:m
C(E3(row,1),E3(row,2)) = E3(row,3);
end
else
eval(['help ' mfilename]);
error('Invalid [V,E] inputs. See help notes above.');
end
end
end
E = a2e(A)
[I,J] = find(A);
E = [I J];
end
D = ve2d(V,E)
VI = V(E(:,1),:);
VJ = V(E(:,2),:);
D = sqrt(sum((VI - VJ).^2,2));
end
end

1 件のコメント

dpb
dpb 2013 年 7 月 26 日
Please edit the code down to only the offending section to the
function [costs,paths] = dijkstra(AorV, ...
line at most...the rest is immaterial to the question/problem.

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

回答 (2 件)

dpb
dpb 2013 年 7 月 26 日

1 投票

clear, clf format short
...
% more script code
...
for i=1:Vertices
for j=1:(i-1)
if Adj_Matrix(i,j)~=0
.....
end;
end;
axis('equal'); axis('off');
function [costs,paths] = dijkstra(AorV,xyCorE,SID,FID,iswaitbar)
error(nargchk(2,5,nargin));
...
As error says, you can't define functions in a script; they must be contained in an m-file of the same name as the function name.
Put the code for the function dijkstra in a file dijkstra.m and then call the function from your script.
doc function % for more details

2 件のコメント

Jan
Jan 2013 年 7 月 26 日
[Relocated from answer section] Ahmzd wrote:
thank you very much. and how can I call the function from the script
Jan
Jan 2013 年 7 月 26 日
I you want the main function to be a script, define the function dijkstra in a new M-file. Or if you want to leave the function definition inside the M-file, convert it to a function.

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

dpb
dpb 2013 年 7 月 26 日

0 投票

Wherever in your script you've defined all of the pertinent inputs for the function and wish to compute the results, then simply enter
[costs,paths] = dijkstra(AorV,xyCorE,SID,FID,iswaitbar);
into the script (or a higher-level function).
User-written functions are no different in their usage than any other Matlab base product or toolbox function in how they're defined/used.

カテゴリ

製品

タグ

タグが未入力です。

質問済み:

2013 年 7 月 26 日

Community Treasure Hunt

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

Start Hunting!

Translated by