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.

cellwrite(fid, cellarray)
function cellwrite(fid, cellarray)
%CELLWRITEAPPEND Write a cell array to a comma separated value file.
%   CELLWRITEAPPEND(FID, C) writes cell array C into FID as comma separated
%   values.
%
%   NOTE: This function is not completely compatible with CSVWRITE.
%   Offsets are not supported and 0 values are not omitted.
%
%   See also CSVWRITE, CSVREAD, DLMREAD, DLMWRITE, WK1READ, WK1WRITE.

% The cell array is traversed, the contents of each cell are converted
% to a string, and a CSV file is written using low level fprintf
% statements, beginning from the end of the file (i.e. appending it).


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

% gets the size of the cell array
[rows, cols] = size(cellarray);

% loops for each row
for i_row = 1:rows
    
    % initialises file_line as something
    file_line = '';
    
    % loops for each column in that row
    for i_col = 1:cols

        % checks the contents of the cell array
        contents = cellarray{i_row, i_col};
        
        % parses contents - note that 
        if isnumeric(contents) || islogical(contents)
            % converts it into a string if its numeric
            contents = num2str(contents);
            
        elseif isempty(contents)
            % if its empty turns it into ''
            contents = '';
        end
        
        % if its not the last column in the row, add another comma to the
        % end - file_line is the growing line to write to the file
        if i_col < cols
            % forms the line with a comma
            file_line = [file_line, contents, ','];
            
        else
            % forms the line without the comma
            file_line = [file_line, contents];
        end
    end
    
    % writes that line to the file
    fprintf(fid, '%s\n', file_line);
end

Contact us