Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to collect info from csv file

Status
Not open for further replies.

rm2

Mechanical
Jun 7, 2005
41
Hello,

I am trying to use the textscan command to work a csv file we generate on a test stand.
First I have 115 strings as headers, for this I thought of:

C_text = textscan(fid, ‘%s’, 115, ‘delimiter’, ‘,’);

I need to ignore/erase the first two. I find it unpractical to write the 113 %s and the two %*s.
Any ideas?

Then I am thinking on getting the results (below each of the 113 strings) , strings and numbers, to start working on them for statistical analysis (histogram, percent failures, codes, etc). For this data, the first 4 are strings, then f f, then s, f f f ..113th f.

Something like this:

‘%s’‘%s’‘%s’‘%s’‘%f’‘%f’‘%s’‘%f’‘%f’‘%f’‘%f’...‘%f’

I do not want to right down the hole chain, any ideas?

I am attaching the file so you can take a look. The final purpose is to pull each file for every day and not waste time trying to make the summary of the results.

I appreciate your help.
thanks
 
Replies continue below

Recommended for you

Found some code.
I will need to change the number of rows depending on the size of the file.

From the data already gathered, I am on a dilema, should I create a separate variable from each column to easly start plotting them or should I work out each of the variables at "data" ? Please take a look to the code and let me know your thoughts. Is it possible to have at the x axis both date/time and Serial Number (obviously, matched) ? how can I change the scale of the y axis ?


function data=dataimport
% Import data from DRC_50HP_Stand1_090406.csv
% Automatically generated 08-Apr-2009

% Define parameters
fileName='C:\Users\Robert\Documents\MATLAB\DRC_50HP_Stand1_090406.csv';
numHeaderLines=7;
formatString='%q%q%q%q%f%f%q%f%f%u64%f%f%f%f%f%f%f%f%f%f%f%u64%f%u64%f%f%f%f%u64%f%f%f%f%f%u64%f%f%f%f%f%f%f%f%f%f%f%u64%u64%u64%f%f%f%f%u64%f%f%f%f%f%u64%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f';
numRows=71;

% Read data from file
fid=fopen(fileName,'rt');
ignoreColumns=zeros(1,0);
columnHeaderRowNumber=4;
columnHeaderRowString=textscan(fid,'%s',1,'headerlines',columnHeaderRowNumber-1,'delimiter','\n');
columnHeaderRow=textscan(columnHeaderRowString{1}{1},repmat('%s',1,113),1,'delimiter',',');
columnNames=columnHeaderRow;
columnNames(ignoreColumns)=[];
dataCell=textscan(fid,formatString,numRows,'headerlines',numHeaderLines-columnHeaderRowNumber,'delimiter',',');

% Convert to 2D Cell
data=cell(numRows+1,length(dataCell)); %Preallocate
data(1,:)=columnNames; % Start with column names

for k=1:length(dataCell)
if iscell(dataCell{k}) % If cell array of strings
data(2:end,k)=dataCell{k};
else % If numerical
data(2:end,k)=mat2cell(dataCell{k},ones(numRows,1),1);
end
end
status=fclose(fid);



I really appreciate your help, thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor