MATLAB Answers

highlight different data on the same graph

1 view (last 30 days)
GIOVANNA GUADAGNIN
GIOVANNA GUADAGNIN on 19 Aug 2021
Edited: Adam Danz on 19 Aug 2021
I create a plot with two different axes
figure
yyaxis left
plot(Tclean.x,Tclean.Temperature,'b-')
ylabel('°t')
yyaxis right
plot(Tclean.x,Tclean.ph,'r-')
ylabel('ph')
Datas come from a table that include temperature, DO and x is a time vector ('2021-07-21 02:10:00' - Format: yyyy-MM-dd HH:mm:ss).
x axis start at 2021-07-21 00:00:00 and it ends at 2021-07-23 08:40:00.
I'd like to highlight a period (from 2021-07-21 02:10:00 to 2021-07-21 08:40:00) on the same graph with a different line.
I thought to creat a loop
hold on
for i = {'2021-07-21 02:10:00'}:{'2021-07-21 08:40:00'}
plot(Tclean.x(i),Tclean.Temperature,'bo')
end
but the output is
%Undefined function 'colon' for input arguments of type 'cell'

Answers (2)

Star Strider
Star Strider on 19 Aug 2021
Try something like this:
Tclean = table(datetime('2021-07-21 00:00:00')+minutes(0:10:1000).', 'VariableNames',{'Time'}); % Create Table
Tclean = [Tclean array2table(rand(height(Tclean), 4))] % Create Table
Tclean = 101×5 table
Time Var1 Var2 Var3 Var4 ____________________ _________ ________ ________ ________ 21-Jul-2021 00:00:00 0.0044181 0.5574 0.32765 0.63126 21-Jul-2021 00:10:00 0.14299 0.73893 0.013102 0.33047 21-Jul-2021 00:20:00 0.24177 0.80681 0.36718 0.44688 21-Jul-2021 00:30:00 0.79409 0.56831 0.12128 0.73734 21-Jul-2021 00:40:00 0.75651 0.66069 0.085325 0.11528 21-Jul-2021 00:50:00 0.84837 0.78757 0.31821 0.47127 21-Jul-2021 01:00:00 0.67077 0.55255 0.21959 0.49633 21-Jul-2021 01:10:00 0.69986 0.066311 0.51486 0.049543 21-Jul-2021 01:20:00 0.59055 0.95615 0.3558 0.51653 21-Jul-2021 01:30:00 0.15497 0.30912 0.34082 0.31738 21-Jul-2021 01:40:00 0.17349 0.28567 0.030185 0.33982 21-Jul-2021 01:50:00 0.78942 0.33723 0.67319 0.098763 21-Jul-2021 02:00:00 0.50649 0.50497 0.20147 0.12196 21-Jul-2021 02:10:00 0.047334 0.53594 0.27033 0.087125 21-Jul-2021 02:20:00 0.94278 0.44081 0.78531 0.23972 21-Jul-2021 02:30:00 0.21584 0.51009 0.65319 0.20165
iv = [datetime('2021-07-21 02:10:00') datetime('2021-07-21 08:40:00')] % Limits
iv = 1×2 datetime array
21-Jul-2021 02:10:00 21-Jul-2021 08:40:00
TcleanVct = (Tclean{:,1} >= iv(1)) & (Tclean{:,1} <= iv(2)); % Logical Vector
figure
plot(Tclean{:,1}, Tclean{:,2}, '-b')
hold on
plot(Tclean{TcleanVct,1}, Tclean{TcleanVct,2}, '--r')
hold off
grid
Make appropriate changes toi get the result you want.
.

Adam Danz
Adam Danz on 19 Aug 2021
Edited: Adam Danz on 19 Aug 2021
It looks like you're using datetime values and if you're not, you should be. Datetime values are not represented by strings. A datetime loop loops through days but your interval is less than a day so it will only execute for the first datetime value. Were you expected it to loop through hours / minutes / or seconds?
Why not just add a colored rectangled that spans the vertical extent of the plot between your selected dates? That will avoid precision errors.
dt = datetime('2021-07-21') + hours(0:24);
data = rand(2,numel(dt));
figure()
yyaxis left
h1 = plot(dt, data(1,:), 'o','DisplayName','LeftData');
yyaxis right
h2 = plot(dt, data(2,:), 'o', 'DisplayName', 'RightData');
interval = datetime({'2021-07-21 02:10:00','2021-07-21 08:40:00'});
yyaxis left
hold on
ax = gca();
ph = patch([interval,fliplr(interval)], ax.YLim([1 1 2 2]), 'k', ...
'FaceAlpha', .1, 'EdgeColor','none', 'DisplayName','Selection');
legend([h1,h2,ph],'Location','SouthOutside', 'orientation','horizontal')

Community Treasure Hunt

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

Start Hunting!

Translated by