How to Insert a Dotted Line using InsertShape?

26 views (last 30 days)
Hello. I'm trying to insert a dotted line onto my figure (A) but struggling with the line spec. Here is my line of code. Normally I'll put 'k--' or 'LineSpec','--' but error says "'LineSpec' is not a recognized parameter". Please can you help me?? Thank you, Sally
A = insertShape(A,'line',[1 168.5 301 168.5],'Color','black','LineWidth',6)

Answers (2)

Abhisek Pradhan
Abhisek Pradhan on 16 Jul 2019
The insertShape MATLAB function doesn’t have the functionality to insert a dotted line over a picture.
You can try a workaround mentioned below.
imshow(A);
hold on;
line([1,301],[168.5,168.5],'Color','r','LineStyle','--','LineWidth',6);
Refer line and insertShape for more information.

Wei Huang
Wei Huang on 8 Nov 2023
Edited: Wei Huang on 8 Nov 2023
Here is a way you can do this by interpolating between the points of your solid line:
% Create some image
I = uint8(zeros(400,400));
% Points defining solid line
solidLine = [1 168.5 301 168.5];
% Interpolating solid line to with n number of points
n = 20;
dashLine = [linspace(solidLine(1),solidLine(3),n);...
linspace(solidLine(2),solidLine(4),n)];
% Account for odd number of interpolated points
if mod(n,2)
n = n-1;
dashLine = dashLine(:,1:end-1);
end
% Reshape dash line for insertShape
dashLine = reshape(dashLine,4,n/2)';
I = insertShape(I,"line",dashLine,'Color','r','LineWidth',6);
imshow(I);

Categories

Find more on Graphics Object Properties in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!