Filter löschen
Filter löschen

Anonymous Functions - how to store the results of anonymous functions

1 Ansicht (letzte 30 Tage)
rng(1) % important for technical reasons
U = [ 1 -1 3 4 ]; % an artificial input sequence
% applyPredictionUpdate returns the predicted state:
applyPredictionUpdate = @(bel, u_t) [bel '.PU(' num2str(u_t) ')'];
% readSensor returns the current sensor's reading:
readSensor = @() [ 'Z(' num2str(round(20*rand())) ')' ];
% applyMeasurementUpdate returns the state updated by the measurements:
applyMeasurementUpdate = @(sensorReading, belBar) [ belBar '.MU(' sensorReading ')' ];
bel = 'PRIOR';
%%%%%%%%%%% PLEASE DON'T CHANGE ANYTHING ABOVE THIS LINE %%%%%%%%%%%
for u_t = U
belBar=@(bel, u_t)[bel,u_t];
sensorReading=readSensor();
bel=applyMeasurementUpdate(sensorReading,belBar);
end
Got the foll. error:
Error using horzcat
The following error occurred converting from char to function_handle:
Too many output arguments.
Error in solution>@(sensorReading,belBar)[belBar,'.MU(',sensorReading,')'] (line 8)
applyMeasurementUpdate = @(sensorReading, belBar) [ belBar '.MU(' sensorReading ')' ];
Error in solution (line 14)
bel=applyMeasurementUpdate(sensorReading,belBar);
" applyPredictionUpdate does not change bel in-place but returns the predicted state, so make sure to store it in a local variable that you can use in applyMeasurementUpdate."
  1 Kommentar
Stephen23
Stephen23 am 6 Apr. 2022
As far as I can tell, defining belBar as a function handle is a red-herring.
rng(1) % important for technical reasons
U = [ 1 -1 3 4 ]; % an artificial input sequence
% applyPredictionUpdate returns the predicted state:
applyPredictionUpdate = @(bel, u_t) [bel '.PU(' num2str(u_t) ')'];
% readSensor returns the current sensor's reading:
readSensor = @() [ 'Z(' num2str(round(20*rand())) ')' ];
% applyMeasurementUpdate returns the state updated by the measurements:
applyMeasurementUpdate = @(sensorReading, belBar) [ belBar '.MU(' sensorReading ')' ];
bel = 'PRIOR';
%%%%%%%%%%% PLEASE DON'T CHANGE ANYTHING ABOVE THIS LINE %%%%%%%%%%%
for u_t = U
sensorReading=readSensor();
bel=applyMeasurementUpdate(sensorReading,[bel,u_t])
end
bel = 'PRIOR.MU(Z(8))'
bel = 'PRIOR.MU(Z(8)) .MU(Z(14))'
bel = 'PRIOR.MU(Z(8)) .MU(Z(14)).MU(Z(0))'
bel = 'PRIOR.MU(Z(8)) .MU(Z(14)).MU(Z(0)).MU(Z(6))'
But the best solution would be to not write code that writes code like that.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

David Hill
David Hill am 6 Apr. 2022
for u_t = U
belBar=applyPredictionUpdate(bel, u_t);
sensorReading=readSensor();
bel=applyMeasurementUpdate(sensorReading,belBar);
end

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 6 Apr. 2022
belBar=@(bel, u_t)[bel,u_t];
That is a function handle
applyMeasurementUpdate = @(sensorReading, belBar) [ belBar '.MU(' sensorReading ')' ];
You are requesting to concatenate the function handle and a character vector.
belBar is a function expecting two inputs, but you are trying to wrap it in a function that takes inputs that do not include the two inputs belBar needs, so you are going to have trouble.
I think it might make more sense to use
belBar = [bel,u_t];

Kategorien

Mehr zu Handle Classes finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by