Plot command inside a loop will not display all data unless drawnow is used
Mostrar comentarios más antiguos
I am working with 2017b.
I have a script that creates a single figure with 3x1 subplots, and it contains a for loop.
Withing the loop, I use errorbar to plot X, Y, Y_error data. Each loop iteration, I plot a single point at each subplot (always in the same axes with the 'hold' property set to 'on').
Sometimes (and it annoys me so much that it's not always), some data points are not displayed in the plots when the script finished running.
However, if I use the 'drawnow()' command after each time I call the errorbar, then all points are displayed.
Is this a known bug, or is it an somewhat expected behaviour?
% CODE MISSING - WHERE VARIABLES ARE DEFINED - BUT HOPEFULLY THIS IS GOOD ENOUGH FOR DEBUGGING
for idxSubject = 1 : numel(uniqueSubjects)
filter_subject = ismember(subjects, uniqueSubjects(idxSubject));
% Creates figures
figureName1 = sprintf('%s', uniqueSubjects{idxSubject});
hFigMeasurementPoints = figure('Position', [0 0 1 1], 'WindowStyle','docked', 'color', 'white', 'Name', figureName1, 'NumberTitle', 'off');
subplot(3,1,1);
for idxLocation = 1 : numel (uniqueLocations)
filter_locations = ismember(locations, uniqueLocations(idxLocation));
% computes the mean and SD of water and bg;
avgW = mean(W(filter_subject&filter_locations), 'omitnan');
avgSNR = mean(snr(filter_subject&filter_locations), 'omitnan');
avgSBR = mean(sbr(filter_subject&filter_locations), 'omitnan');
sdW = std(W(filter_subject&filter_locations), 'omitnan');
sdSNR = std(snr(filter_subject&filter_locations), 'omitnan');
sdSBR = std(sbr(filter_subject&filter_locations), 'omitnan');
set(0, 'CurrentFigure', hFigAveragedData);
subplot(3,1,1); hold on;
errorbar(idxSubject-0.2+0.4*idxLocation/numel(uniqueLocations), avgW, sdW, 's', 'MarkerSize', 10, 'LineWidth', 2, 'Color', myColors(idxLocation, :)); hold on;
title('W');
xlim([0.5, numel(uniqueSubjects)+0.5]);
ylim([65 85]);
xticks(1:numel(uniqueSubjects)); xticklabels(uniqueSubjects);
box on; grid on; grid minor;
legend(uniqueLocations);
subplot(3, 1,2); hold on;
errorbar(idxSubject-0.2+0.4*idxLocation/numel(uniqueLocations), avgSNR, sdSNR, 's', 'MarkerSize', 10, 'LineWidth', 2, 'Color', myColors(idxLocation, :)); hold on;
title('SNR');
xlim([0.5, numel(uniqueSubjects)+0.5]);
ylim([0 500]);
xticks(1:numel(uniqueSubjects)); xticklabels(uniqueSubjects);
box on; grid on; grid minor;
legend(uniqueLocations);
subplot(3, 1,3); hold on;
errorbar(idxSubject-0.2+0.4*idxLocation/numel(uniqueLocations), avgSBR, sdSBR, 's', 'MarkerSize', 10, 'LineWidth', 2, 'Color', myColors(idxLocation, :)); hold on;
title('SBR');
xlim([0.5, numel(uniqueSubjects)+0.5]);
ylim([0 0.12]);
xticks(1:numel(uniqueSubjects)); xticklabels(uniqueSubjects);
box on; grid on; grid minor;
legend(uniqueLocations);
% NOT ALL ITERATIONS WILL DISPLAY THE ERROR BAR POINT IN THE GRAPH IF I DON'T USE DRAWNOW HERE
drawnow();
end
end
2 comentarios
dpb
el 27 de Nov. de 2020
Not a type of symptom have seen, no. Looking at the code might let somebody spot something peculiar in the way the code was written. Is it not possible to vectorize and call errorbar with all the data? That would be more efficient...
riverCN
el 1 de Dic. de 2020
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Creating, Deleting, and Querying Graphics Objects en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!