Create a generic x for (x,y) plot

I'm not well versed in matlab at all so hopefully this post makes some sense.
I have tons of data in .txt files that I want to plot. However the only "x" data set that I have is the real life time which I can't use in my plot(x,y) command since the numbers are 10:23:55, for example. Since the x-axis is just time,I want to make a generic x dataset with a command with which the x gets the same length as the y data sets. Hopefully someone can help me with this

5 Comments

plot(1:numel(y),y)
if the time intervals are equal or convert the times to seconds before plotting.
Sam Chak
Sam Chak on 12 Sep 2024
Edited: Sam Chak on 12 Sep 2024
@Flip Reutelingsperger, One question: Is the y-axis data uniformly sampled in the time span (x-axis data)?
A bit more detail as to what you want to do would be helpful.
It is possible to use interp1 to interpolate datetime vectors —
x = ["10:23:55" "12:20:40"]
x = 1x2 string array
"10:23:55" "12:20:40"
xt = datetime(x, 'InputFormat',"HH:mm:ss", 'Format',"HH:mm:ss")
xt = 1x2 datetime array
10:23:55 12:20:40
y = randn(5,1)
y = 5×1
-0.9841 -0.8324 0.0364 -0.6161 0.8662
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
xp = linspace(xt(1), xt(2), numel(y)).'
xp = 5x1 datetime array
10:23:55 10:53:06 11:22:17 11:51:28 12:20:40
figure
plot(xp, y)
grid
If your ‘x’ vector is longer than your ‘y’ variables, you can simply limit ‘x’ to be the same length as ‘y’, providing that actualy works with your data set.
.
Thank you for your help. I figured out that plot(y) did not work because the data was in tableform. I transformed it to matrix and now I can plot the data without specific x data.
You can still plot with the specifiic ‘x’ data as time data, if you turn the tiime values into a datetime array. (I suspect that they may be read in as a string or cell array and considered text objects. You can import ‘x’ as a datetime array using detectImportOptions or convert them afterwards.)

Sign in to comment.

Answers (2)

Perhaps, you can plot like in this example:
% Time vector
timeStr = ["10:23:55", "10:24:00", "10:24:05", "10:24:10"];
timeNum = datenum(timeStr, 'HH:MM:SS');
% y-axis data
data = [1, 2, 3, 4];
% Plot and label
plot(timeNum, data, 'o--', 'linewidth', 1.5), grid on
datetick('x', 'HH:MM:SS')
Drew
Drew on 12 Sep 2024
Edited: Drew on 12 Sep 2024
MATLAB makes it convenient to use date and/or time data on the x, y, and z axis of your plots. See https://www.mathworks.com/help/matlab/matlab_prog/plot-dates-and-times.html for details and examples. This means you can use x-axis times like "10:23:55" in your plots, if you would like.
From that doc page opening paragraph: "Most plotting functions accept datetime and duration arrays as x-, y-, and z-coordinates and show tick values with appropriate date and time units. You can specify your own axis limits and tick values using datetime and duration values. You can also change the format of tick values to show date and time units of your choice."
Here is an example generated by an LLM using the prompt: Create a matlab plot with times on x axis and either "time until sunset (while sun is up)" or "time until sunrise (when sun is down)" on the y-axis, for Boston MA on 9/12/2024. Use webread from baseURL = 'https://api.sunrise-sunset.org/json'; to get sunrise and sunset times
% Define the API endpoint and parameters
baseURL = 'https://api.sunrise-sunset.org/json';
lat = 42.3601; % Latitude for Boston, MA
lng = -71.0589; % Longitude for Boston, MA
date = '2024-09-12';
% Fetch sunrise and sunset data
response = webread(baseURL, 'lat', lat, 'lng', lng, 'date', date, 'formatted', 0);
sunriseUTC = datetime(response.results.sunrise, 'InputFormat', 'yyyy-MM-dd''T''HH:mm:ss''+00:00''', 'TimeZone', 'UTC');
sunsetUTC = datetime(response.results.sunset, 'InputFormat', 'yyyy-MM-dd''T''HH:mm:ss''+00:00''', 'TimeZone', 'UTC');
% Convert to local time (Boston is UTC-4 in September)
sunriseLocal = datetime(sunriseUTC, 'TimeZone', 'America/New_York');
sunsetLocal = datetime(sunsetUTC, 'TimeZone', 'America/New_York');
% Define time range for the whole day
dayStart = datetime(date, 'Format', 'yyyy-MM-dd', 'TimeZone', 'America/New_York');
dayEnd = dayStart + days(1);
timeRange = dayStart:minutes(1):dayEnd;
% Calculate time until sunrise/sunset
timeUntilSunset = zeros(size(timeRange));
timeUntilSunrise = zeros(size(timeRange));
for i = 1:length(timeRange)
if timeRange(i) < sunriseLocal
timeUntilSunrise(i) = minutes(sunriseLocal - timeRange(i));
elseif timeRange(i) < sunsetLocal
timeUntilSunset(i) = minutes(sunsetLocal - timeRange(i));
else
nextSunrise = sunriseLocal + days(1);
timeUntilSunrise(i) = minutes(nextSunrise - timeRange(i));
end
end
% Plot the results
figure;
hold on;
plot(timeRange, timeUntilSunset, 'b', 'DisplayName', 'Time Until Sunset');
plot(timeRange, timeUntilSunrise, 'r', 'DisplayName', 'Time Until Sunrise');
xlabel('Time of Day');
ylabel('Minutes');
title('Time Until Sunset/Sunrise in Boston, MA on 9/12/2024');
legend;
datetick('x', 'HH:MM', 'keepticks');
grid on;
hold off;
If this answer helps you, please remember to accept the answer.

Categories

Products

Release

R2023b

Tags

Asked:

on 12 Sep 2024

Commented:

on 12 Sep 2024

Community Treasure Hunt

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

Start Hunting!