Peak labeling on spectroscopic data using plotjdx function
6 views (last 30 days)
Show older comments
Natalie Raia
on 25 Nov 2020
Commented: Natalie Raia
on 30 Nov 2020
I am plotting some Raman spectroscopy data, using the plotjdx function created by Micke Malmstrom (here; requires the Bioinformatics Toolbox). This works great, but I would like to find and label the peaks, and cannot figure out where in the script I should do this. My sense is that I need to use the findpeaks function built into MATLAB, but I have yet to incorporate it successfully. Do you have advice on where I could insert the "findpeaks" function and related lines of code?
Here is the plotjdx function. I have attached a test spectra file.
function h=plotjdx(variable,h)
% h=plotjdx(variable,h) Plots a (selection of) jdx file(s) in a new or
% specified figure. If no file is specified then the function ask for user
% to select files...
%
% input 'variable' can be:
% - a 'filename' of a jdx file,
% - a cell with filenames to several jdx files or
% - JCAMPStruct to be plotted in
%
% input figure handle 'h' is optional
%
% BSD micke.malmstrom@gmail.com
% Version 2014-12-18
if nargin==0
% If no input, ask for files
[filename,folderpath] = uigetfile({'*.*'},...
'Select the .jdx files to plot','MultiSelect','on');
% see if handle to fig is specified
if exist('h','var')
h=figure(h);clf, hold on
else % make new figure
h=figure;clf, hold on
end
if iscell(filename)
%its several files
files=filename;
% now plot all files in same figure h
for ii=1:length(filename)
h=plotjdx([folderpath files{ii}],h);
end
legend show
return
else %is only one file so plot it
h=plotjdx([folderpath filename],h);
return
end
% if the input is characters then 'variable' is a filename
elseif ischar(variable)
%import and plot the file
variable=jcampread(variable);
h=plotjdx(variable);
% if the input is a cell then 'variable' is a bunch of filenames
elseif iscell(variable)
% see if handle to fig is specified
if exist('h','var')
h=figure(h);clf, hold on
else % make new figure
h=figure;clf, hold on
end
% now plot all files in same figure h
for ii=1:length(variable)
h=plotjdx([variable{ii}],h);
end
legend show
return
% if the input is a structure then 'variable' is the actual JCAMPStruct
elseif isstruct(variable)
data = variable.Blocks(1);
h=plot(data.XData,data.YData, 'DisplayName',variable.Title);
xlabel(data.XUnits);
ylabel(data.YUnits);
end
2 Comments
Mathieu NOE
on 25 Nov 2020
hello
sure you need findpeaks
there is an example in the documentation for how to plot the peaks
that must be inserted after the plot command , for example in the last section of your code :
% if the input is a structure then 'variable' is the actual JCAMPStruct
elseif isstruct(variable)
data = variable.Blocks(1);
h=plot(data.XData,data.YData, 'DisplayName',variable.Title);
xlabel(data.XUnits);
ylabel(data.YUnits);
end
now, beside that I am a bit surprised to see that this function (plotjdx) call itself in the script
very surprising !!!
Accepted Answer
Tarunbir Gambhir
on 30 Nov 2020
You can try the following:
% if the input is a structure then 'variable' is the actual JCAMPStruct
elseif isstruct(variable)
data = variable.Blocks(1);
h=plot(data.XData,data.YData, 'DisplayName',variable.Title);
findpeaks(data.YData,data.XData,'NPeaks',5,'SortStr','descend');
xlabel(data.XUnits);
ylabel(data.YUnits);
end
Using the above configurations for "findpeaks", you will get the top 5 peaks labeled in the figure.
Alternatively, if you do not want a labeled figure and rather get the peak values and locations, you can provide output while calling the function.
[pk,loc] = findpeaks(data.YData,data.XData,'NPeaks',5,'SortStr','descend');
The peak values and locations will be output in the variables "pk" and "loc" respectively.
For further customization on finding the peaks using this fuction, refer the Mathworks Documentation here.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!