Importing and Displaying USGS Spectral Library Data with MATLAB

The USGS spectral library is an important database of mineral spectra, which is used as a reference for the spectrometric determination of unknown substances. Here is a simple MATLAB program that can be used to load and display the spectra of several minerals.

According to the USGS website, researchers at the USGS Spectroscopy Laboratory have measured the spectral reflectance of thousands of materials in the lab and compiled them in the USGS Spectral Library.  Detailed sample descriptions are provided with the spectra, including the results of X-ray Diffraction, Electron Probe Micro-Analysis, and other analytical methods.

First, we download the desired spectra from the USGS website:

https://www.usgs.gov/labs/spectroscopy-lab/usgs-spectral-library

We save the spectra and wavelengths in directories named spectraldata and wavelengths. We can then load this data using the following MATLAB commands to load the text files containing the wavelengths:

cd wavelengths
filelist_wv = dir(fullfile('*.txt'));
filename_wv = {filelist_wv.name};
for i = 1 : 1
   fidA = fopen(filename_wv{i});
   A1 = textscan(fidA,'%f',...
      'Headerlines',1,...
      'CollectOutput',1);
   fclose(fidA);
   wv(:,i) = A1{1};
end
cd ..
We can then load this data using the following MATLAB commands to load the text files containing the spectra:
cd spectraldata
filelist_spec = dir(fullfile('*.txt'));
filename_spec = {filelist_spec.name};
for i = 1 : length(filelist_spec)
   fidB = fopen(filename_spec{i});
   B1 = textscan(fidB,'%f',...
      'Headerlines',1,...
      'CollectOutput',1);
   fclose(fidB);
   spec(:,i) = B1{1};
end
cd ..
We then create the legend text using
for i = 1 : length(filelist_spec)
   legtex(i,:) = filename_spec{i}(10:14);
end
Then we graphically display the spectra of the desired minerals together with a legend and axis labels using
figure(...
   'Position',[200 200 800 600],...
   'Color',[1 1 1]);
axes(...
   'XLim',[0.25 2.65],...
   'Box','on',...
   'XGrid','On',...
   'YGrid','On',...
   'Units','Centimeters',...
   'Position',[2 2 10 6],...
   'LineWidth',0.6,...
   'FontName','Helvetica',...
   'FontSize',8);
for i = 1 : length(filelist_spec)
line(wv,spec(:,i),...
   'LineWidth',0.75)
end
xlabel('Wavelength (microns)',...
   'FontSize',8)
ylabel('Spectral Reflectance',...
   'FontSize',8)
legend(legtex,'Location','Southwest',...
   'FontSize',8)
legend('boxoff')
We can save the graph in a PNG file using
print -dpng -r300 usgs_spectrallibrary_2025.png
We can then save the data in a binary MATLAB .mat file:
save usgs_spectrallibrary_2025.mat

Downloads

Download MATLAB script and data.

References

Kokaly, R., et al. (2017) USGS Spectral Library Version 7 Data. USGS Asset Identifier Service (AIS). Available online at https://www.usgs.gov/data/usgs-spectral-library-version-7-data
Trauth, M.H. (2021) Signal and Noise in Geosciences, MATLAB Recipes for Data Acquisition in Earth Sciences. Springer International Publishing, 343 p., ISBN 978-3-030-74912-5 (MRDAES)
Trauth, M.H. (2025) MATLAB Recipes for Earth Sciences – Sixth Edition. Springer International Publishing, 550 p., ISBN 978-3-031-57948-6. (MRES)