Borrar filtros
Borrar filtros

Anonymous Functions - how to store the results of anonymous functions

1 visualización (últimos 30 días)
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 comentario
Stephen23
Stephen23 el 6 de Abr. de 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.

Iniciar sesión para comentar.

Respuesta aceptada

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

Más respuestas (1)

Walter Roberson
Walter Roberson el 6 de Abr. de 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];

Categorías

Más información sobre Handle Classes 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