Clear Filters
Clear Filters

How to make a line plot and indicating data gap?

11 views (last 30 days)
Dear,
I have the following datafile (please see the attachement), the data consist of three columnes, Date (YYYY-MM-DD), Time (HH:MM) and the last column is the value which need to be ploted against time. But there is gap in the data for example there is no data between 2020-10-10 to 2020-11-02 and i dont want to connect the line, but instead, i would rather leave a shaded gap and mark it as no data. Do you have any suggestions?
I have tried this code but i had problem combining the data and time; am i on the right path?
data = readtable('data.csv');
dateColumn = data(:, 1);
timeColumn = data(:, 2);
valueColumn = data(:, 3);
% Convert date and time components to serial date numbers and here i get
% error
dateNumbers = datenum(dateColumn{:}, 'yyyy-mm-dd');
timeNumbers = datenum(timeColumn{:}, 'HH:MM');
dateTime = datetime(dateNumbers + timeNumbers, 'ConvertFrom', 'datenum');
plot(dateTime, valueColumn{:});
xlabel('Date and Time');
ylabel('Value');
title('Financial Data');
Example customization options:
grid on;
datetick('x', 'yyyy-mm-dd HH:MM', 'keeplimits');
legend('Value');

Accepted Answer

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 22 May 2023
Edited: Sulaymon Eshkabilov on 22 May 2023
Here is how it can be done. Note that there are only 4 individual data points are missing and thus, they can be displayed individually.
data = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1390854/Data.txt');
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
%data = readtable('Data0.txt');
dateColumn = data(:, 1);
timeColumn = data(:, 2);
valueColumn = data(:, 3);
% Convert date and time components to serial date numbers and here i get
% error
dateNumbers = datenum(dateColumn.date);
timeNumbers = datenum(timeColumn.hr_min, 'HH:MM');
dateTime = datetime(dateNumbers + timeNumbers, 'ConvertFrom', 'datenum');
% Missing data point indices are located
IND = find(ismissing(valueColumn.value));
% Missing data points are taken to be mean of the data in order to display
% in the plot figure
valueColumn.value(IND) = mean(valueColumn.value, 'omitnan');
figure
plot(dateTime(IND), valueColumn.value(IND), 'ro','MarkerFaceColor','y', 'markersize',13);
hold on
plot(dateTime, valueColumn.value, 'b-.');
xlabel('Date and Time');
ylabel('Value');
title('Financial Data');
%Example customization options:
grid on;
hold off
% datetick('x', 'yyyy-mm-dd HH:MM', 'keeplimits');
legend( 'Missing data', 'All existing data');
  2 Comments
Harr
Harr on 23 May 2023
Edited: Harr on 23 May 2023
Dear Sulaymon,
Thank you very much for your effort, unfortunately i cannot run it (is it because i use Matlab 2018b?) and something wrong with the year as well "4043-4044" ! Do you know what was wrong?
/Harr
Sulaymon Eshkabilov
Sulaymon Eshkabilov on 23 May 2023
Use these commands:
data = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1390854/Data.txt');
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
dateColumn = data(:, 1);
timeColumn = data(:, 2);
valueColumn = data(:, 3);
dateNumbers = datenum(dateColumn.date);
timeNumbers = datenum(timeColumn.hr_min, 'HH:MM');
dateTime = datetime(dateNumbers, 'ConvertFrom', 'datenum');
% Missing data point indices are located
IND = find(ismissing(valueColumn.value));
% Missing data points are taken to be mean of the data in order to display
% in the plot figure
valueColumn.value(IND) = mean(valueColumn.value, 'omitnan');
figure
plot(dateTime(IND), valueColumn.value(IND), 'ro','MarkerFaceColor','y', 'markersize',13);
hold on
plot(dateTime, valueColumn.value, 'b-.');
xlabel('Date and Time');
ylabel('Value');
title('Financial Data');
%Example customization options:
grid on;
hold off
% datetick('x', 'yyyy-mm-dd HH:MM', 'keeplimits');
legend( 'Missing data', 'All existing data');

Sign in to comment.

More Answers (1)

Seth Furman
Seth Furman on 5 Jun 2023
datestr is discouraged
I should mention that datestr is discouraged. Prefer datetime where possible.
For example,
dt = datetime("2020-10-01","Format","uuuu-MM-dd")
dt = datetime
2020-10-01
dt.Year, dt.Month, dt.Day
ans = 2020
ans = 10
ans = 1
Consider xregion in MATLAB R2023a or later
% Read data as a timetable
tt = readtimetable("Data.txt", TextType="string", VariableNamingRule="preserve")
tt = 20844×2 timetable
date hr:min value __________ _______ ______ 2020-10-01 "01:00" 9.6179 2020-10-01 "01:26" 9.6321 2020-10-01 "01:30" 9.73 2020-10-01 "01:46" 9.79 2020-10-01 "02:00" 9.8258 2020-10-01 "02:30" 9.8682 2020-10-01 "03:00" 9.7716 2020-10-01 "03:23" 9.7292 2020-10-01 "03:30" 9.4418 2020-10-01 "03:43" 8.8921 2020-10-01 "03:50" 9.1016 2020-10-01 "04:00" 9.1988 2020-10-01 "04:12" 9.2653 2020-10-01 "04:30" 9.3416 2020-10-01 "05:00" 9.2477 2020-10-01 "05:13" 9.203
% Convert duration strings to duration objects
tt.("hr:min") = duration(tt.("hr:min"), InputFormat="hh:mm")
tt = 20844×2 timetable
date hr:min value __________ ________ ______ 2020-10-01 01:00:00 9.6179 2020-10-01 01:26:00 9.6321 2020-10-01 01:30:00 9.73 2020-10-01 01:46:00 9.79 2020-10-01 02:00:00 9.8258 2020-10-01 02:30:00 9.8682 2020-10-01 03:00:00 9.7716 2020-10-01 03:23:00 9.7292 2020-10-01 03:30:00 9.4418 2020-10-01 03:43:00 8.8921 2020-10-01 03:50:00 9.1016 2020-10-01 04:00:00 9.1988 2020-10-01 04:12:00 9.2653 2020-10-01 04:30:00 9.3416 2020-10-01 05:00:00 9.2477 2020-10-01 05:13:00 9.203
% Move duration data to timetable row-times
tt.date = tt.date + tt.("hr:min");
tt.date.Format = "uuuu-MM-dd HH:mm"; % Update datetime format to include hours and minutes
tt.("hr:min") = [] % Remove "hr:min" variable, which is no longer needed
tt = 20844×1 timetable
date value ________________ ______ 2020-10-01 01:00 9.6179 2020-10-01 01:26 9.6321 2020-10-01 01:30 9.73 2020-10-01 01:46 9.79 2020-10-01 02:00 9.8258 2020-10-01 02:30 9.8682 2020-10-01 03:00 9.7716 2020-10-01 03:23 9.7292 2020-10-01 03:30 9.4418 2020-10-01 03:43 8.8921 2020-10-01 03:50 9.1016 2020-10-01 04:00 9.1988 2020-10-01 04:12 9.2653 2020-10-01 04:30 9.3416 2020-10-01 05:00 9.2477 2020-10-01 05:13 9.203
% Plot timetable data and add an xregion with legend entry labeling it as
% having no data
plot(tt, "value");
xregion(datetime(2020,10,10), datetime(2020,11,02), DisplayName="No data");
xtickformat("uuuu-MM-dd HH:mm");
xtickangle(45);
legend(["value" "No data"]);

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!