Plot command inside a loop will not display all data unless drawnow is used
显示 更早的评论
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
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Creating, Deleting, and Querying Graphics Objects 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!