File Exchange

## cline

version 1.0.0.0 (8.38 KB) by Sebastian Hölz

### Sebastian Hölz (view profile)

Draw a colored line in 2D or 3D

Updated 16 Apr 2007

There are several files on the FEX which deal with the plotting of colored lines, but they all have some drawbacks. I wrote a little file, which extends the scope of the submission by Kun Liu (s. below, hoping to deliver a general purpose file, which should work for most purposes.

SYNTAX
======
h = cline(x, y [, z, cdata])

INPUT
=====
x: vector with x-values
y: vector with y-values
z (opt.): vector with z-values
cdata (opt.): vector with color-data

2 input arguments => cdata = y; z=0
3 input arguments => cdata = z
4 i.a. & z = [] => cdata = y; z=0

OUPUT
=====
h: Handle to line (i.e. patch-object !!!)

Examples
========
t = 2*pi:.1:8*pi;

cline(sqrt(t).*sin(t), sqrt(t).*cos(t));
cline(sqrt(t).*sin(t), sqrt(t).*cos(t), t);
cline(sqrt(t).*sin(t), sqrt(t).*cos(t), t, rand(size(t))); view(3)
cline(sqrt(t).*sin(t), sqrt(t).*cos(t), [], rand(size(t))); view(3)

OK, hope you find this file useful

Sebastian

### Cite As

Sebastian Hölz (2020). cline (https://www.mathworks.com/matlabcentral/fileexchange/14677-cline), MATLAB Central File Exchange. Retrieved .

Lucia carichino

Great function!

GreenPiece

### GreenPiece (view profile)

How I can change the range of colors. I use the following command but it does not effect
c = linspace(50, 100, size(data,1));

Tatsuya

### Tatsuya (view profile)

Great function.
Should there be a "||" between lines 64 and 65?
"Consider using newline, semicolon, or comma before this statement for readability."
"Terminate statement with semicolon to suppress output (in functions)."
I think MATLAB is interpreting line 65 as a statement of the IF function rather than as part of the conditional statement.

I am using MATLAB R2017a Windows.

rodrigo Mogollon

Thanks!

SNyk

### SNyk (view profile)

Thank you for the great function! Is it also possible to animate this line?

Sebastian Hölz

### Sebastian Hölz (view profile)

@Dennis: Simple solution:

h = cline(...);
set(h,'Marker','.','LineStyle','none')

Sebastian

Hi Dennis,
If we replace line 82 with
set(p,'cdata', cdata, 'marker', 's', 'edgecolor','interp')
, it will also plot the points.
However, I do not know how to only plot the points and not the line. Maybe Sebastian can comment on his great function.
Thanks.
M

Dennis Honkanen

### Dennis Honkanen (view profile)

Great function,

Is it possible to obtain dots instead of a line?

Regards, Dennis.

William Silva

alessandro

### alessandro (view profile)

Nice function.
Answering to Xavi Alonso (even if comment is quite old), you could insert a matrix mx3 just be reshaping cdata in a way you have mx1x3.
Indeed I have modified cline function replacing rows 76-81 with:

elseif nargin == 4
if isempty(z); z = zeros(size(x)); end
p = patch([x(:); nan], [y(:); nan], [z(:); nan], 0);
cdata = [cdata;nan nan nan];
newcdata=reshape(cdata,size(cdata,1),1,size(cdata,2));
end

it is not optimized maybe but however it worked for me.
Hope this help

Cheers,
Alessandro

Sebastian Hölz

### Sebastian Hölz (view profile)

@Jaap:

Use the following syntax:

fig = figure;
h = cline(...);
set(h,'linewidth',5)

The increased line-width will not be displayed, if you use "painters" as renderer for your figure. Therefore, you will have to change it:

set(fig, 'renderer', 'zbuffer')

- or -

set(fig, 'renderer', 'opengl')

Cheers

Sebastian

Jaap

### Jaap (view profile)

Thanks for the post, I find it very useful. Do you perhaps know a way to increase the thickness of the line? Adding filled circular patch markers does do something, but the markersize makes a very sharp transition at 4.8-4.9.

Natasha

### Natasha (view profile)

Great, works perfectly, thanks

Matteo Niccoli

### Matteo Niccoli (view profile)

This is really a great submission. I can think of vaery many ways of using it.

For starters, in conjunction with Colorspace transforamtions
( www.mathworks.com/matlabcentral/fileexchange/28790-colorspace-transformations ) I used it for an example in my Perceptually improved colormaps submission. I used cline this way:

% J=jet(256);
% LJ=colorspace('RGB->Lab',J);
% figure; cline([1:1:256],LJ(:,1),LJ(:,1),[1:1:256],J);
% title ('L* plot for jet colormap','Color','k','FontSize',12);

Also, as I was reading Steve's post on Lab based colormaps (thank you Steve!), I used cline this way to help me visualize things (notice again I used colorspace):

% theta = linspace(0, pi/2, 256).';
% a = radius * cos(theta);
% b = radius * sin(theta);
% L = linspace(0, 100, 256).';
% Lab = [L, a, b];
% RGBhlx=colorspace('RGB<-Lab',(Lab));
% figure; cline(a, b, L, L,RGBhlx); view(3);
% VIEW(-165,-15);

Sean Torrez

### Sean Torrez (view profile)

Beautiful file, thanks a bunch. Quite simple, works well.

Xavi Alonso

### Xavi Alonso (view profile)

it doesn't work for me when cdata is a matrix

Matt Bennett

##### MATLAB Release Compatibility
Created with R14SP2
Compatible with any release
##### Platform Compatibility
Windows macOS Linux