How to plot line with different line style part way through?

20 views (last 30 days)
Hello,
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? Thanks, Marco

Answers (1)

dpb
dpb on 18 Oct 2015
Edited: dpb on 20 Oct 2015
  1. Put each device x,y data in a separate column in an array and call plot Each column will automagically be a new line with cycling colors
  2. No can do with a single line, linestyle is a property of the entire line, not of the points or line segments along the line. You'll have to break up the above column vectors into multiple arrays spanning the time periods of the mode variable
  3. time data are plotted in Matlab use date numbers of the new datetime object class.
NB: on the point re: multiple sections for a given line and columns in an array. You can fill unused areas within the array w/ NaN which will allow the array to be rectangular as it must be and yet not plot those points.
Alternatively, you can plot the first line, call
hold on
ADDENDUM
BTW, another way to deal with the mode flag would be (particularly if you have all the data a priori for plotting rather than piecemeal) would be to set a logical addressing array to true|false depending on mode and set the alternative locations to NaN as mentioned previously. This will leave you w/ the same sized array for all so the lines are all the same length but not display the NaN locations. and then add subsequent lines to your heart's content.
See the "Getting Started" section in the online doc on graphics for examples to help the learning curve.
ADDENDUM 2
OK, had a few minutes...here a trivial example to illustrate the idea...
First, show all at once...
y=randn(10,3); % some dummy data to plot (3 var's, 10 obs/variable)
plot(y) % plot vs index; define x array for values
legend(num2str([1:3].','Sensor %d')) % label to identify
idx=randi([3,8],1,3); % set an arbitrary spot in each series
mode=true(size(y)); % initialize the mode array
for i=1:3 % set those above that to false
mode(idx(i):end,i)=false;
end
>> mode % example output
mode =
1 1 1
1 1 1
1 1 1
1 0 1
1 0 1
1 0 0
0 0 0
0 0 0
0 0 0
0 0 0
y1=y;y1(~mode)=nan; % for mode 1 use solid; keep 'TRUE'
figure % new figure
plot(y1) % and plot w/ solid line
hold on % to subsequently add to same plot
for i=1:3 % endpoint one less to make continuous line
mode(idx(i)-1:end,i)=false;
end
y2=y;y2(mode)=nan; % opposite group
plot(y2,':') % dotted line
Note that moved the boundary by one so when applied the NaN the second time would pick up the end point of the previous line as the starting point. W/o that, there would be a gap in the displayed line as the dotted line would start an observation after the previous so no line would be drawn between the two segments.

Communities

More Answers in the  ThingSpeak Community

Categories

Find more on Visualize Data in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!