Calling a function in the command window
이전 댓글 표시
Hello everyone, I'm having trouble trying to launch my code through the command window. My whole script consists of a function (attached at the end) which does some stuff; if I try to run the code normally, it doesn't have any issues. As soon as I try to call the function through the command window, it tells me "Unrecognized function or variable 'GNAv2'. Can someone please explain to me what I'm doing wrong?
There are still some modifications to be done to the code, but it's not the main issue now.
Many thanks to whom may help!
function [z_opti,v_opti,z_opts,v_opts] = GNAv2(ploco,vectors)
rng default
[ploco,pwago,vectors,~] = VehiclesToMove('SubTrain','C:\Users\nadal\Downloads\Ottimizzazione Masse Lorenzo Merlino\M2O_D3p3Project\Test\Treno3Loco.txt');
ncomb = 2*(numel(vectors)-numel(ploco));
Z=zeros(1,ncomb);
V=zeros(ncomb,length(vectors));
parfor k=1:ncomb
[~,pwago,vectors,z] = VehiclesToMove('SubTrain','C:\Users\nadal\Downloads\Ottimizzazione Masse Lorenzo Merlino\M2O_D3p3Project\Test\Treno3Loco.txt');
Z(k)=z;
V(k,:)=vectors;
end
fprintf("Fitness Function Values")
disp(Z)
fprintf("Vectors Combinations")
disp(V)
[z_opt,pos]=min(Z)
vector_opt=V(pos,:)
z_opts=z_opt;
v_opts=vector_opt;
for j=1:Inf
% Obtaining the first m/2 combinations deriving from the best
% known solution
pwago = [(2:14) (16:28)];
Copt = zeros(ncomb/2,numel(vectors));
parfor k=1:size(Copt,1)
s=pwago(randi(numel(pwago),1,2));
vector=v_opts(j,:);
vector(s(1))=v_opts(j,s(2));
vector(s(2))=v_opts(j,s(1));
% if ismember(vector,Copt,'rows') == 0
% Copt(k,:)=vector;
% else
% while ismember(vector,Copt,'rows')
% s=pwago(randi(numel(pwago),1,2));
% vector=v_opts(j,:);
% vector(s(1))=v_opts(j,s(2));
% vector(s(2))=v_opts(j,s(1));
% end
% Copt(k,:)=vector;
% end
Copt(k,:)=vector;
end
fprintf("Optimal Offspring")
disp(Copt)
%Fitness Function evaluation of the first m/2 set:
Z_1=zeros(1,size(Copt,1));
for i=1:size(Copt,1)
Settings{1} = 'TechnicalParameters=FromNominalCondition';
Settings{2,1} = ['Permute=[',num2str(Copt(i,:)),']'];
[Flong,Flong10,train,loco,T] = TrainDyPS(1,'','C:\Users\nadal\Downloads\Ottimizzazione Masse Lorenzo Merlino\M2O_D3p3Project\Test\Treno3Loco.txt',0,Settings,'Flong','Flong10','train','loco','T','NoSave');
[RLCF,RLTF] = FromLFToItsRatio(1,1,Flong10,Flong,train,loco,150,T,1,'MinLF');
z = - min(min(RLCF)) + max(max(RLTF));
Z_1(i)=z;
end
fprintf("Fitness Function of closest-to-optimal vectors")
disp(Z_1)
% The remaining m/2 combinations are now calculated, which are computed
% randomly:
[ploco,pwago,vectors,z] = VehiclesToMove('SubTrain','C:\Users\nadal\Downloads\Ottimizzazione Masse Lorenzo Merlino\M2O_D3p3Project\Test\Treno3Loco.txt');
Z_rand=zeros(1,size(Copt,1));
V_rand=zeros(size(Copt,1),length(vectors));
parfor k=1:size(Copt,1)
[ploco,pwago,vectors,z] = VehiclesToMove('SubTrain','C:\Users\nadal\Downloads\Ottimizzazione Masse Lorenzo Merlino\M2O_D3p3Project\Test\Treno3Loco.txt');
Z_rand(k)=z;
V_rand(k,:)=vectors;
end
% Regrouping into m total combinations
C_2=[Copt;V_rand]
Z_2=[Z_1 Z_rand]
z_opti=min(Z_2)
v_opti=C_2(z_opti==Z_2,:)
% Fitness Function Values comparison
if z_opti < z_opts(j)
v_opts = [v_opts; v_opti]
z_opts = [z_opts, z_opti]
else
v_opts = [v_opts; v_opts(j,:)]
z_opts = [z_opts, z_opts(j)]
end
dev=(z_opts(j)-z_opti)/z_opti
hold on
grid on
plot(z_opts,'ro')
xlabel('Iteration Number')
ylabel('Fitness Function Value')
title('Fitness Function Distribution')
if dev < 1e-4 && dev > 0 || dev == 0
break
end
end
hold off
end
채택된 답변
추가 답변 (1개)
Dyuman Joshi
2024년 1월 23일
편집: Dyuman Joshi
2024년 1월 23일
5 개 추천
When you define a function inside a script, it is only accessible within the script.
Restrictions for Local Functions and Variables
Local functions are only visible within the file where they are defined. They are not visible to functions in other files, and cannot be called from the Command Window.
If you want to call a function from command window, you need to define it separately as a function file of its own.
In this way, you can call it inside a script, another function, and directly from the command window as well.
카테고리
도움말 센터 및 File Exchange에서 Toolbox Distribution에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!