I having trouble displaying each value next to the correct data point on stem plot.
29 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Michael Andersson
le 16 Juin 2020
Commenté : Star Strider
le 17 Juin 2020
I have an array of 1x20 containing the peak amplitude values and instead of displaying each text value next to the correct data point, it prints the entire array contents next to every data point.
PC = {0, NaN, -34.30, -39.90, -46.20, -47.80, -55.20, -52.20, -56.80, -60.90, -70.80, NaN, NaN, NaN, NaN, -72.80, NaN, NaN, NaN, NaN}
x=1:1:20;
subplot(3,1,3);
stem(x,RC);
text=text(x, RC, sprintf('%6.3f', RC));
set(text,'Rotation',60,'HorizontalAlignment','left','VerticalAlignment','baseline');
grid;
xlim([0, 20]);
title('Relative contribution of each harmonic ')
xlabel('Odd Harmonics');
ylabel('Contribution');
0 commentaires
Réponse acceptée
Star Strider
le 16 Juin 2020
PC = {0, NaN, -34.30, -39.90, -46.20, -47.80, -55.20, -52.20, -56.80, -60.90, -70.80, NaN, NaN, NaN, NaN, -72.80, NaN, NaN, NaN, NaN}
x = 1:numel(PC);
figure
stem(x, [PC{:}], 'filled')
grid
xlim([0 20])
lbls = compose('%.2f', [PC{:}]);
text(x, [PC{:}], lbls, 'HorizontalAlignment','center', 'VerticalAlignment','top', 'FontSize',8)
2 commentaires
Plus de réponses (1)
Image Analyst
le 16 Juin 2020
I had made up this answer to your duplicate question, when you deleted it and I couldn't post it. So here it is:
v = rand(1, 40);
plot(v, 'b-', 'LineWidth', 2);
grid on;
[peakValues, indexesOfPeaks] = findpeaks(v);
hold on;
stem(indexesOfPeaks, peakValues, 'r.', 'LineWidth', 2, 'MarkerSize', 35);
for k = 1 : length(peakValues)
textLabel = sprintf('(%d, %.2f)', indexesOfPeaks(k), peakValues(k));
% Place text above the point by adding a little bit to the y value.
text(indexesOfPeaks(k), peakValues(k) + 0.05, textLabel, ...
'HorizontalAlignment', 'center', ...
'FontWeight', 'bold', ...
'FontSize', 12, ...
'Color', 'r');
end
We can't run the code you posted here because you didn't include RC, only PC. And if we change PC to RC, it still doesn't run.
Basically you need to put text() into a loop where you put up only one at a time.
Voir également
Catégories
En savoir plus sur Annotations dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!