Can I store a function including multiple variable?
Mostrar comentarios más antiguos
Hi,
Im new to matlab, is it able to store function including multiple variable which still can be used? Something like this
x=linspace(1,10,100);
t=linspace(1,5,50);
f=@(x,t,theta) sin(x+t(a)+theta)
for i=1:10
state{i}=f(:,:,i);
end
where i expect that (but it is not)
state={sin(x+t(a)+1) sin(x+t(a)+2) sin(x+t(a)+3) ... sin(x+t(a)+10)}
so i can used a for loop on t to make a annimation like this
for a=1:length(t)
for b=1:length(state)
plot(x,state{b}(a));
end
end
Is it possible to do something like above or Im using the wrong function?
3 comentarios
Prudhvi Peddagoni
el 22 de Oct. de 2020
can you elaborate more on what you are trying to do?
what is the variable a in the anonymous function?
what should the variable state contain and what should be it's length?
chia ching lin
el 22 de Oct. de 2020
chia ching lin
el 24 de Oct. de 2020
Respuestas (2)
Steven Lord
el 22 de Oct. de 2020
You can't call a function handle using :. If you want the anonymous function to "remember" and use the values of x and t that you've defined in the workspace, you can do that.
x = 1:10;
y = 2;
f = @(theta) y*sin(x+theta);
z1 = f(7)
% check
z2 = 2*sin(8:17)
1 comentario
chia ching lin
el 22 de Oct. de 2020
Walter Roberson
el 22 de Oct. de 2020
for i=1:10
state{i}=@(x,t)f(x,t,i);
end
Note that this will be a cell array of function handles and if you wanted an array of results you would need to cellfun
cellfun(@(F) F(x, y), state)
2 comentarios
chia ching lin
el 24 de Oct. de 2020
Walter Roberson
el 24 de Oct. de 2020
cellfun(@(F) F(x, t), state)
Categorías
Más información sobre Transmitters and Receivers en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!