Can I store a function including multiple variable?
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (2)
Steven Lord
am 22 Okt. 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 Kommentar
Walter Roberson
am 22 Okt. 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 Kommentare
Siehe auch
Kategorien
Mehr zu Transmitters and Receivers finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!