dot indexing in a function handle

Hi all,
Is it possible to use dot indexing in a function handle? Or at least ignore an output?
Please look at the code below.
clear
clc
close
format shortG
P = 100;
vel = 1.5066e+03;
omegab = 0;
L = 30480;
E = 200000;
I = 1.6253e6;
b = 2438;
h = 20;
A = b * h;
rho = 7.8E-9;
jmax = 5;
varMean = [P vel];
varStd = abs([0.15 0.15] .* varMean);
varDist = ["normal","normal"];
g = @(X) 1.76 - beamUnderMovingLoad(X(1),X(2),L,E,I,omegab,rho,A,jmax);
inputs = struct('means', varMean, 'stds', varStd, 'dists', varDist, 'g_func', g, 'N', 10000);
output = monteCarloSimulation(inputs)
output.Pf
output.Beta
It runs properly when the function 'beamUnderMovingLoad()' has only one output, but Id like to select the output in the 'g' function.
Trust all clear!

 Accepted Answer

Rik
Rik on 10 Mar 2023
Edited: Rik on 10 Mar 2023
You will have to write a wrapper that can select an output for you. There is no built-in functionality to do so.
Some like this should work:
function out=output_selector(fun,k,varargin)
% Select the k-th output variable given some input.
% The first input should be a function handle. Any inputs required for the
% function can provided as extra arguments.
out = cell(1,k);
[out{:}]=fun(varargin{:});
out = out{end};
end

8 Comments

Thanks Rik, but it did not work.
I'd like to select for example the 2nd output of 'beamUnderMovingLoad()' to use in the 'g' function handle
What code did you write?
I edited this line
g = @(X) 1.76 - output_selector(beamUnderMovingLoad(X(1),X(2),X(3),E,I,omegab,rho,A,jmax),2,varargin)
It might have helped if you had read the documentation for varargin, which might have given you the idea for how to use it.
The problem is that you provided the output of the function, not the function itself to output_selector.
The line below should do the trick.
g = @(X) 1.76 - output_selector(@beamUnderMovingLoad,2,X(1),X(2),X(3),E,I,omegab,rho,A,jmax)
This way did not work also.
Note the error message: Operator '-' is not supported for operands of type cell.
If I remove the term '1.76 -', it shows 'Conversion to double from cell is not possible'.
Both errors happen when 'g_func' is evaluated in the monteCarloSimulation function
Here is the line:
X = randomNumberGenerator(N, n_vars, dists, means, stds);
G = zeros(N, 1); % Pre-allocate memory for the limit state function
for i = 1:N
G(i) = g_func(X(i,:)); % Evaluate limit state function for each sample
end
Ah, that is the downside of writing that function on mobile. It should have been out = out{end}; instead of out = out(end);.
I'll edit my answer.
Wow!!!
Now it works perfect!
i really appreciate your help
You're welcome

Sign in to comment.

More Answers (0)

Tags

Asked:

on 10 Mar 2023

Commented:

Rik
on 10 Mar 2023

Community Treasure Hunt

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

Start Hunting!