25 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Amjad Iqbal am 22 Mai 2024 um 13:26
Kommentiert: Mathieu NOE am 22 Mai 2024 um 17:04
Akzeptierte Antwort: Voss
- Processed_results.zip
Dear MATLAB Experts,
I have data in .mat files, each corresponding to a particular month with selected_dates and brightness information.
Each month has a different number of output values (1, 2, 3, ..., N). I need to create a stem plot for each month, with the month on the x-axis and brightness on the y-axis.
For example, if D201701 has six values, the plot will show six values in one single stem, and the x-axis will display the month name.
Additionally, I want to annotate each stem value with a marker (e.g., . or *) to indicate the corresponding date. I have attached an image that shows the stem plot for multiple values, as well as the data and code.
Thank you for guidance and help.
2 Kommentare Keine anzeigenKeine ausblenden
Keine anzeigenKeine ausblenden
Cris LaPierre am 22 Mai 2024 um 14:55
Direkter Link zu diesem Kommentar
https://de.mathworks.com/matlabcentral/answers/2121481-to-plot-one-stem-with-multiple-values-for-data-of-different-months#comment_3168956
Are you trying to duplicate the image you shared? If so, I don't see how this is a stem plot, since stem plots would all originate from a common horizontal line. It looks like a bunch of line plots to me.
Amjad Iqbal am 22 Mai 2024 um 15:21
Direkter Link zu diesem Kommentar
https://de.mathworks.com/matlabcentral/answers/2121481-to-plot-one-stem-with-multiple-values-for-data-of-different-months#comment_3169001
Bearbeitet: Amjad Iqbal am 22 Mai 2024 um 15:22
@Cris LaPierre
Yes, I actually wanted to emphasis that I want to plot one stem line for one month to show all the values corresponding to brightness. I understand from image its confusing.
@Voss understood the query and resolved. I appreicate both for time and efforts.
I will further norish the results.
Melden Sie sich an, um zu kommentieren.
Melden Sie sich an, um diese Frage zu beantworten.
Akzeptierte Antwort
Voss am 22 Mai 2024 um 14:57
In MATLAB Online öffnen
- Processed_results.zip
Maybe something along these lines:
unzip('Processed_results.zip')
% load the files
F = dir(fullfile('Processed_results','*.mat'));
NF = numel(F);
for ii = 1:NF
S = load(fullfile(F(ii).folder,F(ii).name));
F(ii).brightness = S.all_brightness_values;
F(ii).dates = S.selected_dates;
end
% count the number of dates and brightnesses in each file
N_dates = arrayfun(@(x)numel(x.dates),F);
N_brightness = arrayfun(@(x)numel(x.brightness),F);
assert(isequal(N_dates,N_brightness))
% collect date and brightness values into matrices;
% each column is a file, each row is a brightness/date
NB = max(N_brightness);
dates = NaT(NB,NF);
brightness = NaN(NB,NF);
for ii = 1:NF
dates(1:N_dates(ii),ii) = F(ii).dates;
brightness(1:N_brightness(ii),ii) = F(ii).brightness;
end
% construct datetime dt representing the start of each month
dt = datetime(year(dates(1,:)),month(dates(1,:)),1);
% markers to be used in plots
markers = '.*xo+^v<>sdph';
markers = repmat(markers,1,ceil(NB/numel(markers)));
% plot stems
figure('Position',[50 50 1500 600])
hold on
for ii = 1:NB
stem(dt,brightness(ii,:),'Marker',markers(ii),'MarkerSize',12)
end
xticks(min(dt):calmonths(1):max(dt))
% or plot markers only
figure('Position',[50 50 1500 600])
hold on
for ii = 1:NB
plot(dt,brightness(ii,:),'LineStyle','none','Marker',markers(ii),'MarkerSize',12)
end
xticks(min(dt):calmonths(1):max(dt))
2 Kommentare Keine anzeigenKeine ausblenden
Keine anzeigenKeine ausblenden
Amjad Iqbal am 22 Mai 2024 um 15:19
Direkter Link zu diesem Kommentar
https://de.mathworks.com/matlabcentral/answers/2121481-to-plot-one-stem-with-multiple-values-for-data-of-different-months#comment_3168996
@Voss I really appreciate your kind efforts and providing the solution.
This is what I wanted to plot and show the results. Once again Thank you.
Voss am 22 Mai 2024 um 15:32
Direkter Link zu diesem Kommentar
https://de.mathworks.com/matlabcentral/answers/2121481-to-plot-one-stem-with-multiple-values-for-data-of-different-months#comment_3169026
You're welcome!
Melden Sie sich an, um zu kommentieren.
Weitere Antworten (1)
Mathieu NOE am 22 Mai 2024 um 16:14
In MATLAB Online öffnen
hello @Amjad Iqbal
I modified a bit your code
added one line in the first for loop
number_of_data_thismonth(k) = numel(brightnessValues); % <= added this line first
then modified the plot
% original plot
% for i = 1:numel(allDates)
% % Plotting the stem for each date
% stem(allDates(i), allBrightnessValues(i), 'filled');
% % Extracting time information
% time_info = datestr(allDates(i), 'HH:MM:SS');
% % Adding time information on top of each stem
% text(allDates(i), allBrightnessValues(i), time_info, 'VerticalAlignment', 'bottom', 'HorizontalAlignment', 'center');
% end
% modified plot
nnn = 1+[0 c*msum(number_of_data_thismonth)]; % cumulative sum of nb of data per month
for i = 1:numel(number_of_data_thismonth)
ind_start = nnn(i);
ind_stop = nnn(i+1) - 1;
ll(i) = ind_stop - ind_start +1; % ll is equal to number_of_data_thismonth, so we have a working code
% Plotting the stem for each date
% x position is "center" data
ind = round(mean(ind_start:ind_stop));
stem(repmat(allDates(ind),ll(i),1), allBrightnessValues(ind_start:ind_stop), 'filled');
% Extracting time information
time_info = datestr(allDates(ind_start:ind_stop), 'HH:MM:SS');
% Adding time information on top of each stem
text(repmat(allDates(ind),ll(i),1), allBrightnessValues(ind_start:ind_stop), time_info, 'VerticalAlignment', 'bottom', 'HorizontalAlignment', 'center');
end
your original plot looks like
now the modified plot is :
try it ; hope it helps - full code below
clear;clc;close;
% List all .mat files in the directory
matFiles = dir('*.mat');
% Initialize cell arrays to hold aggregated data
allBrightnessValues = [];
allDates = [];
% Loop through each .mat file
for k = 1:length(matFiles)
% Load the current .mat file
data = load(matFiles(k).name);
% Extract the data
brightnessValues = data.all_brightness_values;
dates = data.selected_dates;
% Ensure data consistency
if isrow(brightnessValues)
brightnessValues = brightnessValues';
end
number_of_data_thismonth(k) = numel(brightnessValues); % <= added this line first
% Convert dates to cell array of strings if necessary
if ischar(dates)
dates = cellstr(dates);
elseif isdatetime(dates)
dates = cellstr(datestr(dates, 'dd-mmm-yyyy HH:MM:ss'));
elseif iscell(dates)
dates = cellfun(@char, dates, 'UniformOutput', false);
else
error('Unexpected date format in file: %s', matFiles(k).name);
end
% Append the data to the aggregated vectors
allBrightnessValues = [allBrightnessValues; brightnessValues];
allDates = [allDates; dates];
end
% Convert the date strings to datetime format
allDates = datetime(allDates, 'InputFormat', 'dd-MMM-yyyy HH:mm:ss');
% Display the size of the aggregated vectors
disp(['Total number of brightness values: ', num2str(length(allBrightnessValues))]);
disp(['Total number of dates: ', num2str(length(allDates))]);
% Plotting
figure;
hold on;
% original plot
% for i = 1:numel(allDates)
% % Plotting the stem for each date
% stem(allDates(i), allBrightnessValues(i), 'filled');
% % Extracting time information
% time_info = datestr(allDates(i), 'HH:MM:SS');
% % Adding time information on top of each stem
% text(allDates(i), allBrightnessValues(i), time_info, 'VerticalAlignment', 'bottom', 'HorizontalAlignment', 'center');
% end
% modified plot
nnn = 1+[0 c*msum(number_of_data_thismonth)]; % cumulative sum of nb of data per month
for i = 1:numel(number_of_data_thismonth)
ind_start = nnn(i);
ind_stop = nnn(i+1) - 1;
ll(i) = ind_stop - ind_start +1; % ll is equal to number_of_data_thismonth, so we have a working code
% Plotting the stem for each date
% x position is "center" data
ind = round(mean(ind_start:ind_stop));
stem(repmat(allDates(ind),ll(i),1), allBrightnessValues(ind_start:ind_stop), 'filled');
% Extracting time information
time_info = datestr(allDates(ind_start:ind_stop), 'HH:MM:SS');
% Adding time information on top of each stem
text(repmat(allDates(ind),ll(i),1), allBrightnessValues(ind_start:ind_stop), time_info, 'VerticalAlignment', 'bottom', 'HorizontalAlignment', 'center');
end
hold off;
title('Brightness Temperature vs. Date');
xlabel('Date');
ylabel('Brightness Temperature');
datetick('x', 'yyyy-mm-dd', 'keeplimits');
ylim([180 280])
%%
%Plotting
% figure;
% hold on;
% for i = 1:numel(allDates)
% % Plotting the stem for each date
% stem(allDates(i), allBrightnessValues(i), 'filled');
% % Extracting time information
% time_info = datestr(allDates(i), 'HH:MM:SS');
% % Adding time information on top of each stem
% text(allDates(i), allBrightnessValues(i), time_info, 'VerticalAlignment', 'bottom', 'HorizontalAlignment', 'center');
% end
% hold off;
% title('Brightness Temperature vs. Date');
% xlabel('Date'); axis normal;
% ylabel('Brightness Temperature');
% datetick('x', 'yyyy-mm-dd', 'keeplimits');
2 Kommentare Keine anzeigenKeine ausblenden
Keine anzeigenKeine ausblenden
Amjad Iqbal am 22 Mai 2024 um 16:41
Direkter Link zu diesem Kommentar
https://de.mathworks.com/matlabcentral/answers/2121481-to-plot-one-stem-with-multiple-values-for-data-of-different-months#comment_3169096
@Mathieu NOE
I really appreciate the time and efforts to provide the solution. I have modified the plot and I wanted to plot a neat one. Attached one is the finest result. I am thank full for all the experts to respond promptly.
I always get benifit from this platform to learn new ideas and adapt for several solutions.
Mathieu NOE am 22 Mai 2024 um 17:04
Direkter Link zu diesem Kommentar
https://de.mathworks.com/matlabcentral/answers/2121481-to-plot-one-stem-with-multiple-values-for-data-of-different-months#comment_3169126
you're welcome !!
nice plot BTW :)
Melden Sie sich an, um zu kommentieren.
Melden Sie sich an, um diese Frage zu beantworten.
Siehe auch
Kategorien
MATLABEnvironment and Settings
Mehr zu Environment and Settings finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
Es ist ein Fehler aufgetreten
Da Änderungen an der Seite vorgenommen wurden, kann diese Aktion nicht abgeschlossen werden. Laden Sie die Seite neu, um sie im aktualisierten Zustand anzuzeigen.
Website auswählen
Wählen Sie eine Website aus, um übersetzte Inhalte (sofern verfügbar) sowie lokale Veranstaltungen und Angebote anzuzeigen. Auf der Grundlage Ihres Standorts empfehlen wir Ihnen die folgende Auswahl: .
Sie können auch eine Website aus der folgenden Liste auswählen:
Amerika
- América Latina (Español)
- Canada (English)
- United States (English)
Europa
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asien-Pazifik
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Kontakt zu Ihrer lokalen Niederlassung