How to plot line with different line style part way through? | MATLAB | Forum


Please consider registering


— Forum Scope —

— Match —

— Forum Options —

Minimum search word length is 3 characters - maximum search word length is 84 characters

Register Lost password?
sp_Feed sp_TopicIcon
How to plot line with different line style part way through?
New Member
Forum Posts: 4
sp_UserOfflineSmall Offline
October 18, 2015 - 1:46 pm
sp_Permalink sp_Print


I have a few devices sending 3 variables to thingspeak every 30 minutes... device id, temperature, and mode.

I need some help making one graph to display the following:

1) a separate line plotted on the same graph for each deviceID
2) for each line plotted, I would like the line style to change from a solid linestyle when mode is 1 to a dashed linestyle when mode is 0?
3) y axis should represent temperature and x axis should represent the timestamp

I'm new to thingspeak and matlab...the best I have been able to achieve so far is one line on a graph but it has all temperatures and mode from all devices...I haven't been able to separate the lines by deviceID and have no clue if matlab can plot a line using different linestyles *partway* through the line.

Any gurus out there that can point me in the right direction?


Forum Posts: 4
sp_UserOfflineSmall Offline
October 19, 2015 - 8:38 am
sp_Permalink sp_Print


It is possible to plot multiple lines on a single graph in MATLAB using the 'plot' function:

According to the documentation, you would provide your data to the function as a column vector, or matrix, depending on the number of lines to plot.

Lines can be assigned a line style or LineSpec. At this time, it does not appear possible for segments between points to be different LineSpecs. However, it is possible to break the data down into more line segments and assign LineSpecs to each line segment. See the following example in the documentation:

Rob Purser
Natick, MA

Forum Posts: 46
sp_UserOfflineSmall Offline
October 19, 2015 - 10:29 am
sp_Permalink sp_Print


Plotting is a huge subject in MATLAB: See the doc for the plot command here:

There are lots of ways to do this, but here's something to get you started:
% Generate some data
d = table;
d.timestamp = (datetime('now') - minutes(240):minutes(30):datetime('now'))';
d.temperature = [23;45;56;24;44;56;23;43;55]; = [1;2;3;1;2;3;1;2;3];
d.mode = [1;1;1;1;1;1;0;0;0];

% Plot by sensor ID
plot(d.timestamp( == 1),d.temperature( == 1),...
d.timestamp( == 2),d.temperature( == 2),...
d.timestamp( == 3),d.temperature( == 3))

However, it doesn't solve your "dotted when mode is 1" request. You can do this, but it'll take some more work. You'll need to plot the non dotted and dotted separately. So, you'll have 6 lines in your plot.


Senior Development Manager for IoT and Hardware Interfacing for MATLAB at MathWorks.  Visit to explore the IoT Analytic platform that speaks MATLAB made for engineers and scientists.  You can collect, analyze, and act in 5 minutes or less!

Forum Timezone: America/New_York

Most Users Ever Online: 114

Currently Online:
22 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 252

Vinod: 164

cstapels: 96

piajola: 75

vespapierre: 63

Adarsh_Murthy: 59

Newest Members:







Forum Stats:

Groups: 4

Forums: 17

Topics: 1313

Posts: 4565


Member Stats:

Guest Posters: 1

Members: 5703

Moderators: 0

Admins: 2

Administrators: Hans, lee