필터 지우기
필터 지우기

Can you make this source accept a variable declaration

조회 수: 1 (최근 30일)
Isaiah Slemons
Isaiah Slemons 2019년 5월 30일
답변: Walter Roberson 2019년 5월 30일
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
dpb
dpb 2019년 5월 30일
편집: dpb 2019년 5월 30일
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 2019년 5월 30일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2019년 5월 30일
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.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by