Code covered by the BSD License  

Highlights from
Avantes to Matlab

image thumbnail

Avantes to Matlab



11 Jun 2012 (Updated )

Read spectral data from Avantes USB2 spectrometer files

function sampleName = getSampleName(filename)
% GETSAMPLENAME(filename) Attempt to get a sample name (to add to the data
% structure) from the filename. The sample name is either extracted from
% the directory name or from the file name itself. 
% The directory name should be in the form 
% "smth_sampleNAME_whatever_you_like". 
% The NAME part will be used as a sample name. E.g.:
% "2012-11-29_sample324_first_attempt". 
% If extraction from the directory name fails, an attempt is made to
% extract the sample name from the filename with the spectrum. In Avantes
% software, when a new experiment is started, the files will be named like
% "110420_SAMPLE1430001.ROH", where "110420_SAMPLE143" is the user provided
% experiment name and 0001 is the spectrum number. From this file name,
% "143" will also be used as a spectrum sample name (provided that no name
% has been extracted from the directory name).
% If everything fails, the function returns an empty result.

sampleName = [];

[~, name] = fileparts(filename);
directory = cd;
[~, directory] = fileparts(directory);

% Try to get sample name from the directory name
dirNameParts = regexp(directory, '_', 'split');
try %#ok<TRYNC>
    if strcmpi(dirNameParts{2}(1:6), 'sample')
        sampleName = dirNameParts{2}(7:end);

% With directory failed, try to get sample name from the file name
filenameParts = regexp(name, '_', 'split');
try %#ok<TRYNC>
    if strcmpi(filenameParts{2}(1:6), 'sample')
        sampleName = filenameParts{2}(7:end);
        try %#ok<TRYNC>
            if str2double(sampleName) > 999 
                % file number probably follows sample number directly, 
                % as in 110420_SAMPLE1430001.ROH
                sampleName = filenameParts{2}(7:9);

Contact us