Create a generic x for (x,y) plot

33 visualizaciones (últimos 30 días)
Flip
Flip el 12 de Sept. de 2024
Comentada: Star Strider el 12 de Sept. de 2024
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 comentarios
Flip
Flip el 12 de Sept. de 2024
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.
Star Strider
Star Strider el 12 de Sept. de 2024
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.)

Iniciar sesión para comentar.

Respuestas (2)

Sam Chak
Sam Chak el 12 de Sept. de 2024
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 el 12 de Sept. de 2024
Editada: Drew el 12 de Sept. de 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.

Categorías

Más información sobre Dates and Time en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by