Plot two outputs (or two columns of one output) of a function against each other using only one line of script in an anonymous function
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have the following anonymous functions (this is a much-simplified version for the sake of asking this question) that both produce a new x and y array, which I'd like to plot in a single command (so it can be called within another anonymous function). Function @xy1 outputs two arrays (x and dy), while function @xy2 outputs a single array with x and dy as columns. It doesn't matter which approach is used, they're just the two methods I can come up with and I'm hoping one of them is do-able.
x=[1:10]';
y=rand(10,1);
xy1=@(x,y) deal(diff(y)./diff(x), x(1:end-1)+diff(x)./2);
xy2=@(x,y)[diff(y)./diff(x) x(1:end-1)+diff(x)./2];
[xnew,dy]=xy1(x,y);
xy=xy2(x,y);
I'd like to get the new x and dy arrays to plot as they would if you used plot(xnew,dy), but I need them to plot directly from the function call, something like:
newfunction1=@(x,y) plot(xy1(x,y));
or
newfunction2=@(x,y) plot(xy2(x,y)(:,1),xy2(x,y),(:,2));
Obviously these don't work, but I'm looking for a way to do something similar. Any suggestions would be appreciated. Thanks!
0 commentaires
Réponse acceptée
Walter Roberson
le 23 Jan 2018
Given those restrictions, it would be possible to plot using the xy2 function together with a messy subsref() call for the x axes, and then calling xy2 again with a different messy subsref() for the y axes data.
But it would sure be easier and clearer and more efficient if it were permitted to use two lines of anonymous functions, with the function calling the other.
3 commentaires
Walter Roberson
le 29 Jan 2018
plotxy_helper = @(xy) plot(xy(:,1), xy(:,2))
plotxy2 = @(x, y) plotxy_helper(xy2(x, y));
This invokes xy2 once and receives the result back as a nameless array, which is then passed to plotxy_helper which pulls it apart to use the parts.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Holidays / Seasons 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!