필터 지우기
필터 지우기

How to add text on the figure without unknown x and y coordinates ?

조회 수: 14 (최근 30일)
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 ?

채택된 답변

Image Analyst
Image Analyst 2021년 6월 23일
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
Onur Hakverdi
Onur Hakverdi 2021년 6월 23일
편집: Image Analyst 2021년 6월 23일
That is awesome job. I just only wanted a percentage value on the figure, but logic is true. Instead of "The Max Intensity is 209.45", I wanted "The percentage of is %90.xx" like this. I couldn't explain my problem very well, so probably people were thinking wrong.
Image Analyst
Image Analyst 2021년 6월 23일
@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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Scott MacKenzie
Scott MacKenzie 2021년 6월 23일
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
Onur Hakverdi
Onur Hakverdi 2021년 6월 23일
m file is here but it requires csv file.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Import and Export에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by