too many input arguments error when i pass struct to my function

5 views (last 30 days)
I am getting too many input arguments error when i pass struct to my function... struggling since last 2 days.... help requested....my called function and calling matlab script both files are attached :::
  2 Comments
per isakson
per isakson on 14 May 2021
Edited: per isakson on 14 May 2021
"many input arguments error when i pass struct to my function" I fail to reproduce any such error on R2018b.
First I ran SwarmOptimization14May2021 and encountered
Undefined function or variable 'Vehiclen'.
Error in SwarmOptimization14May2021 (line 79)
U(Vehiclen)
Then I commented out the two final lines
U(Vehiclen)
t(Vehiclen)
and ran SwarmOptimization14May2021 again. This time without errors.
All calls of CostTimeDCal1 are commented out in SwarmOptimization14May2021
muhammad ilyas khattak khattak
Edited: muhammad ilyas khattak khattak on 14 May 2021
@per isaksonlot of thanks for your time and quick reply..... here is the script of 'SwarmOptimization14May2021' for which i am getting error
% ......Particle swarm optimization...... for computation-intensive task
% offloading ..... scripting start date:: 03 May 2021
% For computing-intensive task offloading, the process can be regarded as different
% task particles to choose their best location and to quickly search for the location
% set in accordance with the scene under different conditions
% Thus, the multi-objective optimization problem is formulated as.....
N_Total = 40;
U = zeros(N_Total,1);
t = zeros(N_Total,1);
Lambda = sym('Lambda',[N_Total,1]);
%Lambda = optimvar('Lambda',N_Total,1);
%Alphal = optimvar('Alphal',N_Total,1);
VarAlpha = sym('VarAlpha',[N_Total,1]);
Betam = sym('Betam',[N_Total,1]);
% Betam = optimvar('Betam',N_Total,1);
fl = sym('fl',[N_Total,1]);
% fl = optimvar('fl',N_Total,1);
fe = sym('fe',[N_Total,1]);
% fe = optimvar('fe',N_Total,1);
% function [U t] = CostTimeDCal(Vehiclen,N_Total,U,t,Lambda,Betam,Alphal,fl,fe)
n = 1;
% fl = sym('fl',[N_Total,1]); % Processing capability needed for task Tn is fl(n) and maximum processing capability of CAV n Fn.....
% pl(n) = kn*fl(n)^3; % Keeping it equal to 1 Watt at the moment as recommmended in the article......
Fn = 1.4*10^9; % Fn initialized to value illustrated in article by author ............
% fe = sym('fe',[N_Total,1]); % Processing capability needed for task Tn is fe(n) and maximum processing capability of RES n Fe.....
Fe = 3*10^9; % Fe initialized to value illustrated in article by author ............
if (n == 1)
%subs(Alphal(n),Alphal(n),1);
VarAlpha(n) = sym(1);
Betam(n) = sym(1);
Lambda(n) = sym(rand(1,1));
fe(n) = sym(0.4*10^9);
fl(n) = sym(0.4*10^9);
end
% subs(x(Vehiclen),x(Vehiclen),Lambda(Vehiclen))
%x(Vehiclen) = Lambda(Vehiclen)
% CostTimeDelayCalHandle = @CostTimeDCal;
% f = @(x)CostTimeDCal;
% x = optimvar('x',N_Total,1); % Optimization variable....
x = struct('n',n,'U',U(n),'t',t(n),'Lambda',Lambda(n),'Betam',Betam(n),'VarAlpha',VarAlpha(n),'fl',fl(n),'fe',fe(n)) % Structured variable....
% x = struct('n',[1:N_Total];'U',U;'t',t;'Lambda',Lambda;'Betam',Betam;'VarAlpha',VarAlpha;'fl',fl;'fe',fe); % Structured variable....
% x = struct('n' [1:N_Total]; 'U' U; 't' t; 'Lambda' Lambda; 'Betam' Betam; 'VarAlpha' VarAlpha; 'fl' fl; 'fe' fe);
% x = struct('n',[1:N_Total],'U',U,'t',t,'Lambda',Lambda,'Betam',Betam,'VarAlpha',VarAlpha,'fl',fl,'fe',fe);
x.n(n) = n; % current vehicle...
x.U(n) = U(n);
x.t(n) = t(n);
x.Lambda(n) = Lambda(n);
x.Betam(n) = Betam(n);
x.VarAlpha(n) = VarAlpha(n);
x.fl(n) = fl(n);
x.fe(n) = fe(n);
x
%f = @(x.n(n), x.U(n), x.t(n), x.Lambda(n), x.Betam(n), x.VarAlpha(n), x.fl(n), x.fe(n))CostTimeDCal1;
% f = @(x(1).n, x.U, x.t, x.Lambda, x.Betam, x.VarAlpha, x.fl, x.fe)CostTimeDCal1;
% f = @(n, U, t, Lambda, Betam, VarAlpha, fl, fe)CostTimeDCal1;
% f = @(n, U(n), t(n), Lambda(n), Betam(n), VarAlpha(n), fl(n), fe(n))CostTimeDCal1;
% f = @(n, Un, tn, Lambdan, Betamn, VarAlphan, fln, fen)CostTimeDCal1;
f = @(x)CostTimeDCal1;
% NARGIN
% NARGOUT
% [U,t] = f(n,U,t,Lambda,Betam,VarAlpha,fl,fe);
% [U,t] = f(n(n),U(n),t(n),Lambda(n),Betam(n),VarAlpha(n),fl(n),fe(n));
[Ur,tr] = f(x.n(n),x.U(n),x.t(n),x.Lambda(n),x.Betam(n),x.VarAlpha(n),x.fl(n),x.fe(n));
% [Ur,tr] = f(x, x.n(n) x.U(n) x.t(n) x.Lambda(n) x.Betam(n) x.VarAlpha(n) x.fl(n) x.fe(n));
% [Ur,tr] = f(x.n,x.U,x.t,x.Lambda,x.Betam,x.VarAlpha,x.fl,x.fe);
% [Ur,tr] = f(x);
% [Ur,tr] = f(n(n),U(n),t(n),Lambda(n),Betam(n),VarAlpha(n),fl(n),fe(n));
% U(Vehiclen)
% t(Vehiclen)

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 14 May 2021
f = @(x)CostTimeDCal1;
That says that when f is called, it should ignore its single input parameter and call CostTImeDCal1 with no input parameters. Are you sure that is what you want?
[Ur,tr] = f(x.n(n),x.U(n),x.t(n),x.Lambda(n),x.Betam(n),x.VarAlpha(n),x.fl(n),x.fe(n));
and there you call f with 8 input parameters. But f only expects one input parameter (which it would ignore anyhow.)
The code that is most likely for this kind of situation is:
f = @CostTImeDCal1;
and
[Ur,tr] = f([x.n(n),x.U(n),x.t(n),x.Lambda(n),x.Betam(n),x.VarAlpha(n),x.fl(n),x.fe(n)]);
  1 Comment
muhammad ilyas khattak khattak
@Walter Roberson once again many thanks walter for your time and fast reply.....i am calling this function to be used in one of the optimization problem.... ..... given in the attachment....given your expertie, if you get time, kindly share with me any relevant suggestions/guidance to script in matalb for solving an optimization problem of the type attached... any needed further equations, feel free to contact....if you dont get time, its alright.... your above answer is of much help....

Sign in to comment.

More Answers (0)

Categories

Find more on Argument Definitions in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!