Borrar filtros
Borrar filtros

Calling a function in the command window

33 visualizaciones (últimos 30 días)
Lorenzo Merlino
Lorenzo Merlino el 23 de En. de 2024
Editada: Hassaan el 23 de En. de 2024
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

Respuesta aceptada

Hassaan
Hassaan el 23 de En. de 2024
Editada: Hassaan el 23 de En. de 2024
  1. File Naming and Location: Ensure that the function GNAv2 is saved in a MATLAB file with the exact same name, GNAv2.m, and that this file is in the current MATLAB path or in a directory that MATLAB has access to. You can check the current directory using the pwd command and change it with cd <directory_path> if needed.
  2. File Path: If the file is in a different directory than your current MATLAB working directory, you need to add that directory to the MATLAB path. You can do this by using the addpath('directory_name') command, where 'directory_name' is the path to the directory containing GNAv2.m.
  3. Function Declaration: Make sure the first line in your GNAv2.m file exactly matches your function declaration, including the function name and the input-output syntax. Any discrepancy here can cause MATLAB to not recognize the function properly.
  4. MATLAB Session: Sometimes, MATLAB might not update its function path cache immediately after adding new functions or changing directories. Try running rehash toolboxcache or restarting MATLAB.
  5. Syntax Errors: Syntax errors usually prevent a script from running entirely, it's worth double-checking that there are no obvious syntax issues in your function.
  6. Nested Functions or Local Scope: Ensure that GNAv2 is not a nested function within another function or script. If it is, MATLAB won't recognize it from the command window, as its scope would be local to the parent function or script.
After ensuring these points, try calling your function from the command window again. For example, if ploco and vectors are your inputs, you would call it like this:
[z_opti, v_opti, z_opts, v_opts] = GNAv2(ploco, vectors);
Remember to replace ploco and vectors with actual inputs that your function expects.

Más respuestas (1)

Dyuman Joshi
Dyuman Joshi el 23 de En. de 2024
Editada: Dyuman Joshi el 23 de En. de 2024
When you define a function inside a script, it is only accessible within the script.
From this documentation page Local functions in scripts -
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.

Categorías

Más información sobre Entering Commands en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by