Filter löschen
Filter löschen

Can you make this source accept a variable declaration

1 Ansicht (letzte 30 Tage)
I'm using a community add-on called scatterbar3 and I need to be able to call it a variable but the way the source is written it's currently unable to do that. Thanks in advance.
Source:
function scatterbar3(X,Y,Z,width) % Original
% function var = scatterbar3(X,Y,Z,width) What I'd like to be able to do.
[r,c]=size(Z);
for j=1:r,
for k=1:c,
if ~isnan(Z(j,k))
drawbar(X(j,k),Y(j,k),Z(j,k),width/2)
end
end
end
zlim=[min(Z(:)) max(Z(:))];
if zlim(1)>0,zlim(1)=0;end
if zlim(2)<0,zlim(2)=0;end
axis([min(X(:))-width max(X(:))+width min(Y(:))-width max(Y(:))+width zlim])
caxis([0 50])
function drawbar(x,y,z,width)
h(1)=patch([-width -width width width]+x,[-width width width -width]+y,[0 0 0 0],'b');
h(2)=patch(width.*[-1 -1 1 1]+x,width.*[-1 -1 -1 -1]+y,z.*[0 1 1 0],'b');
h(3)=patch(width.*[-1 -1 -1 -1]+x,width.*[-1 -1 1 1]+y,z.*[0 1 1 0],'b');
h(4)=patch([-width -width width width]+x,[-width width width -width]+y,[z z z z],'b');
h(5)=patch(width.*[-1 -1 1 1]+x,width.*[1 1 1 1]+y,z.*[0 1 1 0],'b');
h(6)=patch(width.*[1 1 1 1]+x,width.*[-1 -1 1 1]+y,z.*[0 1 1 0],'b');
set(h,'facecolor','flat','FaceVertexCData',z)
  2 Kommentare
dpb
dpb am 30 Mai 2019
Bearbeitet: dpb am 30 Mai 2019
Well, what do you want it to return? You can have it return whatever you want but it's your call as to what that should be.
What would you intend to do with the return value(s) if you had it(them)?
Isaiah Slemons
Isaiah Slemons am 30 Mai 2019
I want it to return a variable to me that I can set to User Data inside of a struct. I basically want to be able to turn the bar plot on and off.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 30 Mai 2019
function all_h = scatterbar3(X,Y,Z,width) % Original
[r,c]=size(Z);
all_h = gobjects(r, c, 6);
for j=1:r,
for k=1:c,
if ~isnan(Z(j,k))
all_h(j, k, :) = drawbar(X(j,k),Y(j,k),Z(j,k),width/2)
end
end
end
zlim=[min(Z(:)) max(Z(:))];
if zlim(1)>0,zlim(1)=0;end
if zlim(2)<0,zlim(2)=0;end
axis([min(X(:))-width max(X(:))+width min(Y(:))-width max(Y(:))+width zlim])
caxis([0 50])
function h = drawbar(x,y,z,width)
h(1)=patch([-width -width width width]+x,[-width width width -width]+y,[0 0 0 0],'b');
h(2)=patch(width.*[-1 -1 1 1]+x,width.*[-1 -1 -1 -1]+y,z.*[0 1 1 0],'b');
h(3)=patch(width.*[-1 -1 -1 -1]+x,width.*[-1 -1 1 1]+y,z.*[0 1 1 0],'b');
h(4)=patch([-width -width width width]+x,[-width width width -width]+y,[z z z z],'b');
h(5)=patch(width.*[-1 -1 1 1]+x,width.*[1 1 1 1]+y,z.*[0 1 1 0],'b');
h(6)=patch(width.*[1 1 1 1]+x,width.*[-1 -1 1 1]+y,z.*[0 1 1 0],'b');
set(h,'facecolor','flat','FaceVertexCData',z)
However, since you do not draw bars for nan values, there will be locations where the placeholder graphics objects will not be overwritten with patch objects, so you cannot just blindly set(all_h, 'visible', 'off')
Note that the code is inefficient and should ideally only draw a single patch object per invocation of drawbar.

Weitere Antworten (0)

Kategorien

Mehr zu Data Distribution Plots 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!

Translated by