Class constructor says "not enough input arguments"
3 views (last 30 days)
Show older comments
I am trying to implement a particle filter and run it on saved data. One function "pf2_1" loops through the data and uses a particle filter constructed in the class "Model". It is in a separate class for organization purposes.
When I try and construct Model, I get the following error:
Not enough input arguments.
Error in pf2_1 (line 52)
model = Model(sensorPositions, gsd_, np_, psf_, rsm_);
I added underscores after those inputs for fear I was having a name conflict. No matter what I try I cannot get past the construction. Below are my files:
pf2_1:
% D is [secs, mins, raw data... raw data, state est 1... state est n]
%function [avgDist, avgAng, stddevDist, stddevAng, D] = doPF(dataFile, aggFname, directory, params)
function [avgDistMean, avgAngMean, stddevDistMean, stddevAngMean, avgDistMax, avgAngMax, stddevDistMax, stddevAngMax, D] = pf2_1(simFile, makePlot, gsd_, wbd, psf_, np_, sem, rsm_)
%clear all
%close all
%fclose('all');
delete(instrfindall) % For open serial ports
% Output file
outputname = '';
% Declare # of receivers
NUM_RECEIVERS = 6;
sensorPositions = zeros(2, NUM_RECEIVERS);
% Plot or no plot?
PLOT = makePlot;
myPlot = [];
% Live or simulated?
LIVE = false;
if (LIVE)
% Specify beacon locations
% initialize beacons (do all this in beacon-handling class?)
% collect garbage
% ENSURE ALL OF THEM ARE TALKING TO YOU
% They are talking? Good, start up everything else
% Create file to log to (inside file handling class!!)
else
sim_file = simFile;
data = csvread(sim_file);
%disp(size(sensorPositions(1,:)));
%disp(size(data(1,2:2+NUM_RECEIVERS)));
sensorPositions(1,:) = data(1,3:2+NUM_RECEIVERS);
sensorPositions(2,:) = data(2,3:2+NUM_RECEIVERS);
%disp(sensorPositions);
% Create model (includes particle filter, mapping RSSI-m, etc...
% ====================== ERROR OCCURS HERE ======================
model = Model(sensorPositions, gsd_, np_, psf_, rsm_);
%model = Model();
%model.begin(sensorPositions, gsd_, np_, psf_, rsm_);
% Create plot class
if (PLOT)
myPlot = PlotClass();
myPlot.begin(max(sensorPositions(1,:)), max(sensorPositions(2,:)), ...
sensorPositions, model.bound, ...
LIVE); % Not live if in this branch, leave for ease of reuse
end
disp('starting loop');
distMeanList = []; % Can predetermine size for improved speed
angleMeanList = [];
distMaxList = [];
angleMaxList = [];
D = [];
% Loop over data
for s=3:length(data)
[statePred, covPred] = predict(model.pf, model.noise);
%disp('exited predict');
secs = data(s, 1);
mins = data(s, 2);
raws = data(s, 3:2+NUM_RECEIVERS);
x_truth = data(s, 3+NUM_RECEIVERS);
y_truth = data(s, 4+NUM_RECEIVERS);
%mapped = model.RSSI_TO_M_COEFF * exp(model.RSSI_TO_M_EXP * data(s, 3:2+NUM_RECEIVERS));
mapped = model.RSSI_TO_M_COEFF * exp(model.RSSI_TO_M_EXP * raws);
%[stateCorrected, covCorrected] = correct(model.pf, data(s, 3:2+NUM_RECEIVERS), sensorPositions);
[stateCorrected, covCorrected] = correct(model.pf, mapped, sensorPositions);
%disp('exited correct');
%disp(stateCorrected);
% Get dist from truth
diff = sqrt((stateCorrected(1)-y_truth)^2 + (stateCorrected(2)-y_truth)^2);
%disp(data(s, 3+NUM_RECEIVERS:4+NUM_RECEIVERS));
%disp(stateCorrected);
angle = atan2(x_truth-stateCorrected(1), y_truth-stateCorrected(2));
D = [D, [stateCorrected, covCorrected]];
distMeanList = [distMeanList, diff];
angleMeanList = [angleMeanList, angle];
notNeeded, idx = max(model.pf.Weights);
diffMax = sqrt((model.pf.Particles(idx,2)-y_truth)^2 + (model.pf.Particles(idx,1)-y_truth)^2);
angleMax = atan2(x_truth-model.pf.Particles(idx,1), y_truth-model.pf.Particles(idx,2));
distMaxList = [distMaxList, diffMax];
angleMaxList = [angleMaxList, angleMax];
%disp(diff);
%disp(angle);
%disp(sprintf('%f, %f', diff, angle));
%rawString = sprintf('%d, ', raws);
% secs, mins, raw data, x truth, y truth, x meas, y meas, diff, angle
%toPrint = sprintf('%f,%f,%s,%d,%d,%d,%d,%f,%f', secs, mins, rawString, x_truth, y_truth, stateCorrected(1), stateCorrected(2), diff, angle);
%disp(toPrint);
%fprintf(fid, '%s', toPrint);
%fprintf(fid, '\n');
if (PLOT)
%myPlot.updatePlotSim(model.pf, data(s,1), stateCorrected, data(s, 2+NUM_RECEIVERS:3+NUM_RECEIVERS));
myPlot.updatePlotSim(model.pf, data(s,1), mapped, sensorPositions, stateCorrected, [x_truth, y_truth]);
disp('updated plot');
end
end
avgDistMean = mean(distMeanList);
avgAngMean = mean(angleMeanList);
stddevDistMean = std2(distMeanList);
stddevAngMean = std2(angleMeanList);
avgDistMax = mean(distMaxList);
avgAngMax = mean(angleMaxList);
stddevDistMax = std2(distMaxList);
stddevAngMax = std2(angleMaxList);
end
end
Model:
classdef Model < handle
% Holds all the tuable parameters of the particle filter
properties(SetAccess = public)
% The particle filter
%pf = robotics.ParticleFilter % Is this allowed here?
pf
% # of particles in the filter
NUM_PARTICLES = 1000
% Boundaries of the system, square with edge 2*bound
bound = 10
% sensor positions (x, y)
sensorPositions
% Noise parameter
noise = 0.1
% State estimation method
sem = 'mean' % or 'maxweight'
% Resampling mehtod 'multinomial' or 'systematic'
rsm = 'systematic'
% Gaussian stddev in mlf
gsd
% RSSI to m conversion coeffs
RSSI_TO_M_COEFF = 0.00482998
RSSI_TO_M_EXP = -0.104954
end
properties(SetAccess = private)
end
methods
% function obj = Model(sensorPositions, gsd, numParticles, psf, rsm)
%function obj = Model()
% %pf = robotics.ParticleFilter; % Do stuff like this in the
% %properties
% obj.pf = robotics.ParticleFilter;
%
% obj.gsd = gsd;
% obj.NUM_PARTICLES = numParticles;
% obj.noise = psf;
% %obj.sem = sem;
% obj.rsm = rsm;
%
% stateBounds = [
% -obj.bound, obj.bound;
% -obj.bound, obj.bound];
% initialize(obj.pf, obj.NUM_PARTICLES, stateBounds);
%
% obj.pf.StateEstimationMethod = 'mean'; % is this allowed in the properties?
% obj.pf.ResamplingMethod = obj.rsm;
%
% obj.pf.StateTransitionFcn = @stf1_1;
% obj.pf.MeasurementLikelihoodFcn = @mlf1_1;
%
% obj.sensorPositions = sensorPositions;
% Do nothing?
%end
%function begin(obj, array)
%function begin(obj, sensorPosition, gsd, numParticles, psf, rsm)
function obj = Model(sensorPosition_, gsd_, numParticles_, psf_, rsm_)
%[sensorPosition, gsd, numParticles, psf, rsm] = array;
% sensorPosition = array(0);
% gsd = array(1);
% numParticles = array(2);
% psf = array(3);
% rsm = array(4);
obj.pf = robotics.ParticleFilter;
obj.gsd = gsd_;
obj.NUM_PARTICLES = numParticles_;
obj.noise = psf_;
%obj.sem = sem;
obj.rsm = rsm_;
stateBounds = [
-obj.bound, obj.bound;
-obj.bound, obj.bound];
initialize(obj.pf, obj.NUM_PARTICLES, stateBounds);
obj.pf.StateEstimationMethod = 'mean'; % is this allowed in the properties?
obj.pf.ResamplingMethod = obj.rsm;
obj.pf.StateTransitionFcn = @stf1_1;
obj.pf.MeasurementLikelihoodFcn = @mlf1_1;
obj.sensorPositions = sensorPosition_;
end
function [stateCorrected, covCorrected] = correct(pf, measurement, sensorPositions)
[stateCorrected, covCorrected] = correct(pf, measurement, sensorPositions, obj.gsd);
end
function [statePred, covPred] = predict(pf, NOISE)
[statePred, covPred] = predict(pf, NOISE);
end
end
end
As you can see I have also tried an initialization function for Model (begin) instead of doing everything in the constructor and received the same error. I also tried passing the variables in an array instead of one at a time and received the same "Not enough inputs" error.
If you see any other ways I can improve my organization, efficiency, etc your suggestions are appreciated.
4 Comments
Adam
on 2 Nov 2017
You only get an error when you try to use one of the input arguments. If you never use it Matlab doesn't care if you pass it in or not. This is how you can have optional arguments to a function.
So if you don't use an input argument until line 200 you won't get the error until then if it doesn't exist.
This is one of the reasons I use
doc validateattributes
in most of my public functions for every input argument, together with narginchk and if exist(...) and ~isempty(...) type checks on all my input arguments so that I will get these errors at the top of a function rather than further down where their cause may be less obvious
Accepted Answer
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!