How to add text on the figure without unknown x and y coordinates ?
14 views (last 30 days)
Show older comments
Onur Hakverdi
on 23 Jun 2021
Commented: Image Analyst
on 23 Jun 2021
Hi i have a csv file the problem is my data has random x,y values so it changes according to machine but i want to display a value on the figure. I tried something like this v = (intensity/intensity_max)*100; text(x,y,(v) but it didn't work because of the x and y values. How can i overcome on this problem ?
Accepted Answer
Image Analyst
on 23 Jun 2021
The problem was you were printing an entire array of thousands of elements instead of a single number.
This seems to work. Adapt as needed.
% Demo to print the peak on a spectrogram.
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
close all; % Close all figures (except those of imtool.)
clearvars;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 18;
inputFolder = pwd;
fullFileName = fullfile(inputFolder, 'spectroscopy.csv')
data = csvread(fullFileName);
wavelength = data(:,2);
intensity = data(:,1);
[intensity_max, index] = max(intensity)
wavelength_max = wavelength(index);
plot(wavelength,intensity, 'b-');
hold on;
plot(wavelength_max, intensity_max, 'ro', 'MarkerSize', 11, 'LineWidth', 2);
xlabel('Wavelength', 'FontSize', fontSize);
ylabel('Intensity', 'FontSize', fontSize);
grid on;
% Compute normalized intensity vector, but strangely enough, we never use it.
normalizedIntensity = (intensity / intensity_max) * 100;
% Print max value next to the peak of the curve.
xText = wavelength_max
yText = intensity_max
textLabel = sprintf(' The Max Intensity is %.7f', intensity_max)
text(xText, yText, textLabel, 'horizontalalignment', 'left', 'Color', 'r', 'FontWeight', 'bold', 'FontSize', 14);
% Give a title.
if 333800 < intensity_max && intensity_max < 345100
title('Cu-I', 'FontSize', fontSize)
elseif 862000 < intensity_max && intensity_max < 90550
title('Al-I', 'FontSize', fontSize)
elseif 4200 < intensity_max && intensity_max < 5720
title('C-I', 'FontSize', fontSize)
elseif 209 < intensity_max && intensity_max < 211
title('C-II', 'FontSize', fontSize)
else
title('Unknown species', 'FontSize', fontSize);
end
outputFolder = 'C:\Plots\';
if ~isfolder(outputFolder)
% Create folder if it dows not exist.
mkdir(outputFolder);
end
fullOutputFileName = fullfile(outputFolder, 'Plot.png')
% saveas(gcf, fullOutputFileName); % Old: pre R2020a
% exportgraphics(gca, fullOutputFileName); % New: R2020a or newer
fprintf('Done!\n');
2 Comments
Image Analyst
on 23 Jun 2021
@Onur Hakverdi, of course the peak is at 100%, and all the rest of the thousands of values are like 50% or so. And surely you don't want thousands of text labels on there - you wouldn't be able to see your data.
If you don't want what I put, then you can change sprintf() to have it say exactly what you want. Just don't put the whole array name into sprintf() or it will try to use each one of those thousands of array elements over and over again with the format string.
More Answers (1)
Scott MacKenzie
on 23 Jun 2021
To print 'hello' in the center of the figure... (adjust accordingly)
ax = gca;
x = ax.XLim(1) + (ax.XLim(2) - ax.XLim(1)) / 2;
y = ax.YLim(1) + (ax.YLim(2) - ax.YLim(1)) / 2;
text(x, y, 'hello', 'horizontalalignment', 'center');
10 Comments
See Also
Categories
Find more on Environment and Settings in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!