Code covered by the BSD License  

Highlights from
xPC File Scope Tools

image thumbnail

xPC File Scope Tools

by

 

26 Jul 2007 (Updated )

Functions to easily save xPC File Scope data to the target PC as MAT files.

xpcdata2csv(xpcdata,FileName)
function xpcdata2csv(xpcdata,FileName)
% xpcdata2mat - Convert xpcdata format to a matrix
% -------------------------------------------------------------------------
% Abstract: This function converts the xpcdata structure format to a CSV
% file.
%
% Syntax:
%       xpcdata2csv(xpcdata)
%       xpcdata2csv(xpcdata,FileName)
%
% Inputs:
%           xpcdata - xpcdata structure generated from getxpcFileData.m
%           FileName - output filename (.csv file)
%
% Outputs:
%           none
%
% Examples:
%           none
%
% Notes: none
%

% Copyright 2007 - 2009 The MathWorks, Inc.
%
% Auth/Revision:
%   The MathWorks Consulting Group
%   $Author: rjackey $
%   $Revision: 10 $  $Date: 2007-07-26 15:56:29 -0400 (Thu, 26 Jul 2007) $
% -------------------------------------------------------------------------


% Check input arguments
if nargin < 1
    error('Must give xpcdata structure as an input.');
end

% Prompt for file name
if nargin < 2
    [FileName, PathName] = uiputfile('*.csv','Save xpcdata to CSV format','xpcdata.csv');

    % If the user cancelled, just return
    if isequal(FileName,0)
        return;
    end

else
    PathName = pwd;
end

% Combine the path and filename
FullFilePath = fullfile(PathName,FileName);

% Convert the structure to an array
[Data,Time,Signals] = xpcdata2mat(xpcdata);

% Combine Time and Data together
Data = [Time,Data];

% Write header to CSV file
fid = fopen(FullFilePath,'w');
if isequal(fid,-1)
    error(['Unable to create file ' FullFilePath]);
end
try
    for i=1:length(Signals)
        fprintf(fid,'%s,',Signals{i});
    end
    fprintf(fid,'\n');
    fclose(fid);
catch
    fclose(fid);
    rethrow(lasterror);
end

% Write data to CSV file
dlmwrite(FullFilePath,Data,'-append');

Contact us