LLcsv - %opens the file to read into it ln =...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
function ca = LLcsv(ch) % In MATLAB, we've learned the built-in csvread function, which reads in % numeric data from a comma separated value file and outputs the numeric % data in an array. However, csvread cannot handle files with non-numeric % data. Write a function called LLcsv that will take in a csv file and % return all the data from the file as strings in a cell array. Numbers % may appear in the csv data, but they can be kept as strings. % The layout of the data in the text file should be preserved in the cell % array. Data appearing on the second line of the csv file, for % instance, should appear in the second line of the cell array. You may % need to use empty cells to complete a square cell array. %usage: ca = LLcsv(ch) fh = fopen(ch,'r');
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: %opens the file to read into it ln = fgetl(fh); %initializes the ln to an empty string count = 1; %starts the count at 0 tcount = 1; %starts the token count at 0 ca = {}; %creates an empty cell array while ischar(ln) %initial condition [tok rest] = strtok(ln,','); %tokenizes the line while ~isempty(tok) %checks to see if token is an empty string ca{count,tcount} = tok; %indexes the rows and columns and places the token into a %cell at that position tcount = tcount + 1; %changes the column position [tok rest] = strtok(rest,','); %terminating condition end ln = fgetl(fh); %terminating condition count = count + 1; %adds one to the count (row) tcount = 1; %re-initializes our token count at 1 (columns) end fclose(fh); end...
View Full Document

This note was uploaded on 10/21/2010 for the course CS 1371 taught by Professor Stallworth during the Spring '08 term at Georgia Tech.

Ask a homework question - tutors are online