Calling a function in the command window (2024)

18 views (last 30 days)

Show older comments

Lorenzo Merlino on 23 Jan 2024

  • Link

    Direct link to this question

    https://support.mathworks.com/matlabcentral/answers/2073191-calling-a-function-in-the-command-window

  • Link

    Direct link to this question

    https://support.mathworks.com/matlabcentral/answers/2073191-calling-a-function-in-the-command-window

Edited: Hassaan on 23 Jan 2024

Accepted Answer: Hassaan

Open in MATLAB Online

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;

% 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)

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

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

Accepted Answer

Hassaan on 23 Jan 2024

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/2073191-calling-a-function-in-the-command-window#answer_1395416

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/2073191-calling-a-function-in-the-command-window#answer_1395416

Edited: Hassaan on 23 Jan 2024

Open in MATLAB Online

  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.

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

More Answers (1)

Dyuman Joshi on 23 Jan 2024

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/2073191-calling-a-function-in-the-command-window#answer_1395431

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/2073191-calling-a-function-in-the-command-window#answer_1395431

Edited: Dyuman Joshi on 23 Jan 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.

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

See Also

Categories

MATLABLanguage FundamentalsEntering Commands

Find more on Entering Commands in Help Center and File Exchange

Tags

  • function
  • command window

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


Calling a function in the command window (4)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

Contact your local office

Calling a function in the command window (2024)
Top Articles
Latest Posts
Article information

Author: Reed Wilderman

Last Updated:

Views: 5740

Rating: 4.1 / 5 (72 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Reed Wilderman

Birthday: 1992-06-14

Address: 998 Estell Village, Lake Oscarberg, SD 48713-6877

Phone: +21813267449721

Job: Technology Engineer

Hobby: Swimming, Do it yourself, Beekeeping, Lapidary, Cosplaying, Hiking, Graffiti

Introduction: My name is Reed Wilderman, I am a faithful, bright, lucky, adventurous, lively, rich, vast person who loves writing and wants to share my knowledge and understanding with you.