Code covered by the BSD License  

Highlights from
CO2gui - lab control and automation

CO2gui - lab control and automation

by

 

06 Jan 2010 (Updated )

Software used for controlling and data logging lab equipment.

fibreimport(fileName, channels)
function data = fibreimport(fileName, channels)
% FIBREIMPORT imports the data into MATLAB from the fibre optic trace files

% checks the number of arguments
error(nargchk(0, 2, nargin))

% checks inputs
if ~nargin
    % bring up a dialog to find the file
    [fileNamePartial, pathName] = uigetfile('*.dat');

    % checks that something was selected
    if isequal(fileNamePartial, 0) && isequal(pathName, 0)
        % errors
        error('You must select a file to open.')
    end

    % forms the filename properly
    fileName = [pathName, fileNamePartial];

else
    % checks that the supplied file exists
    if ~exist('file', fileName)
        % errors
        error('Supplied filename does not exist.')
    end
end

% if the number of channels was supplied check its valid
if nargin >= 2
    % check the channels
    if ~isequal(channels, 1) && ~isequal(channels, 2)
        % errors
        error('Invalid number of fibre optic channels.')

    else
        % read in the correct number of channels fast using textscan

        % opens the file
        fid = fopen(fileName);

        % checks it opened
        if fid == -1
            % error
            error('Could not open file.')
        end

        % reads in the entire file (assumes its one channel only)
        data = textscan(fid, '%n%n');

        % unpacks the data
        data = data{1};
    end

else
    % read it in more slowly using textread, but it counts the correct
    % number of columns
    data = textread(fileName);

    % checks the number of columns - it should be two or three
    if size(data, 2) ~= 2 && size(data, 2) ~= 3
        % warning
        warning('fibreimport:readError', 'Number of columns is not two - possibly corrupt or incorrect data.')
    end
end

% finds the index of the first time when its negative to correct for
% overrun issues - currently only works
negativeIndexes = find(data(:, 1) < 0);

% adds on an offset to fix things - firstly normalises to 0 ("adds" the
% first negative number on), adds the last normal number back on, then
% assumes the spacing between the number is always linear, so adds one
% data spacing on as well
data(negativeIndexes, 1) = data(negativeIndexes, 1) - data(negativeIndexes(1), 1) + (2 * data(negativeIndexes(1) - 1, 1)) - data(negativeIndexes(1) - 2, 1);

% closes the file at the end of the number of channels was specified
% (since we used textscan instead)
if nargin <= 2
    % closes the file
    status = fclose(fid);

    % checks it closed the file correctly
    if ~status
        % gives a warning
        warning('fibreimport:fileCloseError', 'Did not close file properly.')
    end
end

Contact us