# Entering values into a function handle output

1 view (last 30 days)
Rick on 13 Aug 2014
Answered: Geoff Hayes on 13 Aug 2014
Hello, I have a function, and I can calculate P from the command window, but when I use this function, my output yh has P(1), P(2), etc, but what I want it do to is replace those with the actual value calculated
function yh = BreakRod(cellData)
Y = cellData{1,:}';
U = cellData{2,:}';
V = cellData{3,:}';
P = A\Y;
yh = @(U,V) P(1) + P(2)*exp(U.^2) + P(3)*sqrt(V) + P(4)*U.*V;
end
N = size(U,1);
A = ones(N,4);
A(:,2) = exp(U.^2);
A(:,3) = sqrt(V);
A(:,4) = U.*V;
end
cellData{:}
ans =
1 4 5 6
ans =
1.0000 2.0000 3.0000 0.5000
ans =
2 1 7 3
P
P =
30.4842
0.0299
-3.4979
-12.3093
yh = BreakRod(cellData)
yh =
@(U,V)P(1)+P(2)*exp(U.^2)+P(3)*sqrt(V)+P(4)*U.*V

Geoff Hayes on 13 Aug 2014
Rick - you can use the str2func function to convert a string to a function. Just build the function string by concatenating parts of the equation and use num2str to convert the P numeric data to strings
yh = str2func(['@(U,V) ' num2str(P(1)) ' + ' num2str(P(2)) ...
'*exp(U.^2) + ' num2str(P(3)) '*sqrt(V) + ' ...
num2str(P(4)) '*U.*V']);
And then
yh = BreakRod(cellData)
yh =
@(U,V)30.4842+0.029898*exp(U.^2)+-3.4979*sqrt(V)+-12.3093*U.*V
Try the above and see what happens!