# Create cell array of handle functions, which have been created from 2x1 double arrays

2 vues (au cours des 30 derniers jours)
Kraka Doros le 3 Juil 2022
Modifié(e) : Kraka Doros le 3 Juil 2022
% i want to create an array of 2x1 cells, for example :
% @(t)1*t+3
% @(t)2*t+4
% So i write :
>> clear
f={@(t)1*t+3
@(t)2*t+4};
% But i do not want to write the two functions, manually.
% I want to create this array, from pre-existing data,
% for example :
>> clear
a=[1 2]';
b=[3 4]';
% The above gives me, two 2x1 double array.
% a =
% 1
% 2
% b =
% 3
% 4
% Now, if i will write :
>> for ff=1:2
f{ff}=@(t)a(ff)*t+b(ff);
end
>> f=f';
% i will receive a 2x1 cell :
% f =
% @(t)a(ff)*t+b(ff)
% @(t)a(ff)*t+b(ff)
% f =
% @(t)1*t+3
% @(t)2*t+4
% Any help please, how to create it.
##### 1 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
Jan le 3 Juil 2022
Your message is hard to read. Please use the tools for formatting. Type code as code and test as text. Posting code as text and the text as pseudo-comments reduce the readability.

Connectez-vous pour commenter.

### Réponse acceptée

Jan le 3 Juil 2022
a = [1; 2];
b = [3; 4];
f = cell(2, 1);
for ff = 1:2
f{ff} = str2func(sprintf('@(t) %d * t + %d', a(ff), b(ff)));
end
f
f = 2×1 cell array
{@(t)1*t+3} {@(t)2*t+4}
##### 1 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
Kraka Doros le 3 Juil 2022
I apologies if my writing cause problems, for somebody trying read my question. I will do my best, in future.
So i will accept it.
Thanks.

Connectez-vous pour commenter.

### Plus de réponses (1)

patrick1704 le 3 Juil 2022
Hi there,
I am not sure if I understand the problem correctly because once you evaluate the function handle Matlab, you will get the desired result. So what is the point of having it explicity written in the code?
Matlab will store the variables a and b as well as the index ff in the local function workspace, so you can even do something like this once you created the handles:
>> clear a b
>> f{1}(1)
ans =
4
>> f{2}(1)
ans =
6
The fact that Matlab does not do the actually value-representation is not really resolvable from my perspective, except for maybe when doing some ugly eval expression:
str2func(['@(t)',num2str(eval('a(ff)')),'*t+',num2str(eval('b(ff)'))])
However, I would not recommend it as the other solution works perfectly fine.
##### 1 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
Kraka Doros le 3 Juil 2022
My question was a part of creating a larger code. I just wanted to create, a cell array, from some handle functions which have been created, by themselves, from 2x1 double arrays.
I did not want the results but just the general form of the functions.
Anyway thanks for your interest to help me.

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Numeric Types dans Help Center et File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by