Group multiple lines to one "object" and be able to "toggle" on or off

Hi, I have this functioj that draws a grid on an image
function drawGrid(RowSize,ColSize,IM,ax)
% Rowsize=512
% Colsize=256
[height,width]=size(IM);
nRows=floor(height/RowSize);
nCols=floor(width/ColSize);
hold(ax,"on");
for i=1:nRows %plot Col Lines
plot(ax,[1 width],[i*RowSize i*RowSize],'r','LineWidth',0.2)
end
for i=1:nCols %Plot Row Lines
XX=i*floor(width/nCols);
plot(ax,[XX XX],[1 height],'r','LineWidth',0.2)
end
hold(ax,"off");
end
I want to be able to toggle the whole grid on or off once its drawn. So I was wondering
1: How to group all the lines to one object?
2: If I pass that "object" as an output, how can I toggle on or off.
Thanks
Jason

 Réponse acceptée

hello
To group multiple lines into a single "object" in MATLAB, you can use a hggroup or hgtransform object.
here your code updated with hggroup
im = imread('your_image_here.jpg');
figure
imshow(im)
RowSize=40; % whatever number you want
ColSize=40; % whatever number you want
group = drawGrid(RowSize,ColSize,im,gca);
% Toggle visibility of the group
set(group, 'Visible', 'off'); % Hide all lines in the group
% and
set(group, 'Visible', 'on'); % Show all lines in the group
%%%%%%%%%%%%
function group = drawGrid(RowSize,ColSize,IM,ax)
% Rowsize=512
% Colsize=256
[height,width,p]=size(IM);
nRows=floor(height/RowSize);
nCols=floor(width/ColSize);
hold(ax,"on");
% Group the lines into a single object
group = hggroup; % create group
for i=1:nRows %plot Col Lines
prow{i} = plot(ax,[1 width],[i*RowSize i*RowSize],'r','LineWidth',0.2) ;
set(prow{i} , 'Parent', group);
end
for i=1:nCols %Plot Row Lines
XX=i*floor(width/nCols);
pcol{i} = plot(ax,[XX XX],[1 height],'r','LineWidth',0.2);
set(pcol{i} , 'Parent', group);
end
hold(ax,"off");
end

8 commentaires

Thats exactly what Im after - I hadn't heard of hggroup before. Thanks
Mathieu NOE
Mathieu NOE le 29 Sep 2025
Modifié(e) : Mathieu NOE le 29 Sep 2025
Tx
I also discovered that quite recently ! :)
Jason
Jason le 29 Sep 2025
Modifié(e) : Jason le 29 Sep 2025
Actually, there something not quite right, On my axes "ax" I have an image
as soon as I equate the plot to something (like this)
prow{i} = plot(ax,[1 width],[i*RowSize i*RowSize],'r','LineWidth',0.2) ;
the lines are not plotted on my axes that I pass in i.e.ax, instead a new blank figure opens up.
But if i do this, it does plot on the image which is on axes ax.
plot(ax,[1 width],[i*RowSize i*RowSize],'r','LineWidth',0.2) ;
Why is that?
also would this be an alternative approach
h=findobj('Type','line')
Mathieu NOE
Mathieu NOE le 30 Sep 2025
Modifié(e) : Mathieu NOE le 30 Sep 2025
which matlab release are you working with ?
I tested my code with R2025a
could you share your code or a working example of your issue ?
Hi, Im using 2024b. Here is my code
function group=drawGrid(RowSize,ColSize,IM,ax)
% IM is the image
% ax is the uiaxes to display the lines on (which also has the image IM)
[height,width]=size(IM);
nRows=floor(height/RowSize);
nCols=floor(width/ColSize);
hold(ax,"on");
% Group the lines into a single object
group = hggroup; % create group
for i=1:nRows %plot Col Lines
prow{i}=plot(ax,[1 width],[i*RowSize i*RowSize],'r','LineWidth',0.2);
set(prow{i} , 'Parent', group);
end
for i=1:nCols %Plot Row Lines
XX=i*floor(width/nCols);
pcol{i}=plot(ax,[XX XX],[1 height],'r','LineWidth',0.2);
set(pcol{i} , 'Parent', group);
end
hold(ax,"off");
end
And this is how I call it
clc;
ax=app.UIAxes; ax2=app.UIAxes2; cla(ax2,"reset"); CL=ax.CLim;
IM=getimage(ax); [sy,sx]=size(IM);
RemoveAnnotations(app,ax); RemoveAnnotations(app,ax2);
xl=400; xr=10650; yt=0; yb=0; % For image crop
n=numel(app.imgArray); % app.imgArray = my images
data=[];
for i=1:n
% Get Image from ImageStack (already created)
app.ImSpinner.Value=i;
data(i,1)=i;
IM=app.imgArray{i}; % This is where my images are!
[sy,sx]=size(IM);
if i==1
cla(ax,"reset");
app.myimage=imagesc(ax, IM); colormap(ax,'gray'); axis(ax,'image'); set(ax,'xtick',[]); set(ax,'ytick',[]);
else
% This is faster to view images by using CData
RemoveAnnotations(app,ax);
set(app.myimage, 'CData', IM); % Update the image data using handle app.myimage (much faster)
end
% Crop Image & autoscale
rect=[xl, yb, xr-xl,sy-yt-yb]; %[x, y, width, height]
hold(ax,'on')
rectangle(ax,'Position',rect, 'EdgeColor','y','LineWidth',1);
IM=imcrop(IM,rect); cla(ax,"reset");
myImagesc(app, ax, IM); cl=[0.15,0.15,0.2]; ax.Color=cl; drawnow;
[sy,sx]=size(IM);
% Create Block array % make the y direction 512 pixels
ngridscol=floor(sx/512);
ngridsrow=floor(sy/512);
ReportMessage(app,[num2str(i),' # of Rows: ',num2str(ngridsrow)]);
% Perform BlockProc Measurements
[blockFwhm,blockFwhm1,blockFwhm2]=IQBlockProc_FOV(app,ax,ax2,ngridsrow,ngridscol,IM,CL);
med=mean2(blockFwhm); mediandev=3*std2(blockFwhm);
data(i,2)=med; data(i,3)=mediandev;
end
where
function RemoveAnnotations(app,ax)
clc;
h1 = findall(ax, 'type', 'line');
h2 = findall(ax, 'type', 'text');
h3 = findall(ax, 'type', 'rectangle');
delete(findall(ax,'type', 'Marker')); %Delete markers
delete(findall(ax,'Type','images.roi.Line'));
h4 = findall(ax, 'type', 'ConstantLine');
delete(h1); delete(h2); delete(h3); delete(h4);
and
function [blockFwhm,blockFwhm1,blockFwhm2]=IQBlockProc_FOV(app,ax,ax2,ngridsrow,ngridscol,IM,CL)
% % % ngridsrow=19; % was 20
% % % ngridscol=19; % Number of blocks in each direction ngridsrow=100; ngridscol=1;
[sy,sx]=size(IM);
RowSize=floor(sy/ngridsrow); ColSize=floor(sx/ngridscol);
app.myGrid=drawGrid(RowSize,ColSize,IM,ax);
blockSizeRow=RowSize;
blockSizeCol=ColSize;
blockImages = splitImageIntoBlocks(IM, blockSizeRow, blockSizeCol);
[nBlocksRow, nBlocksCol] = size(blockImages);
% setup waitbar for info:
hwait = waitbar(0, 'Please wait... ','Position',[10 50 300 50],'Name','Working on Block...','CreateCancelBtn','setappdata(gcbf,''canceling'',1)'); %left, bottom, width, height
l=nBlocksRow*nBlocksCol;
indx=1; ct=0;
xoff=floor(blockSizeCol/2);
yoff=floor(blockSizeRow/2);
blockFwhm = zeros(nBlocksRow, nBlocksCol); % Holder for variable 1
blockFwhm1 = zeros(nBlocksRow, nBlocksCol); % Holder for variable 2
blockFwhm2 = zeros(nBlocksRow, nBlocksCol); % Holder for variable 3
data=[];
midblockCol=round(nBlocksCol/2)+1;
for blockIdxRow = 1 : nBlocksRow
% Check for clicked Cancel button
if getappdata(hwait,'canceling')
break
end
for blockIdxCol = 1 : nBlocksCol
if (blockIdxCol~=1) &&(blockIdxCol~=midblockCol) &&(blockIdxCol~=nBlocksCol) %Just get ends and central regions
indx=indx+1; ct=ct+1;
continue
end
waitbar(indx/l,hwait,[num2str(indx),'/',num2str(l)]);
indx=indx+1; ct=ct+1;
drawnow;
blockImage = double(blockImages{blockIdxRow, blockIdxCol});
blockImage=blockImage-min(blockImage(:)); % Background subtract
try
% Make measurements on BlockImage
sd=std2(blockImage);
FM = std2(blockImage)^2/mean2(blockImage);
Bren=fmeasure(blockImage,'BREN',[]); % Focus metric
[~,ImQS]=IQCalc(app,blockImage,0); % another focus metric
catch
blockFwhm(blockIdxRow, blockIdxCol) = NaN;
blockFwhm1(blockIdxRow, blockIdxCol) = NaN;
blockFwhm2(blockIdxRow, blockIdxCol) = NaN;
continue;
end
if ct==1
cla(ax2,"reset");
app.myimage=imagesc(ax2,blockImage); %image creation
colormap(ax2,'gray'); axis(ax2,'image'); set(ax2,'xtick',[]); set(ax2,'ytick',[]);
ax2.CLim=CL;
else
RemoveAnnotations(app,ax2);
set(app.myimage, 'CData', blockImage); % Update the image data using handle app.myimage (much faster)
end
blockFwhm(blockIdxRow, blockIdxCol) = ImQS;
blockFwhm1(blockIdxRow, blockIdxCol) = sd; %FM;
blockFwhm2(blockIdxRow, blockIdxCol) =Bren; %fwhmBlockY;
% Show current Block location analysed on raw image
xx=blockSizeCol*blockIdxCol-xoff;
yy=blockSizeRow*blockIdxRow-yoff;
hold(ax,"on");
plot(ax,xx,yy,'c.','MarkerSize',20); drawnow;
end
end
delete(hwait);
% Remove all columns with zeros
blockFwhm = blockFwhm(:,any(blockFwhm,1)); blockFwhm1 = blockFwhm1(:,any(blockFwhm1,1)); blockFwhm2 = blockFwhm2(:,any(blockFwhm2,1));
hmm, it's going to be difficult for me to help you
app's are not really my area of expertise and I probably will not be able to reproduce & run your test environment on my side
what is the error message you get ?

Ok thsnks for your help and introducing me to hggrouo.

I think i have an easier way, but have run into another issue, so i will post a new question.

ok good luck !!

Connectez-vous pour commenter.

Plus de réponses (0)

Produits

Version

R2024b

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by