LLcsv - %creates an empty cell array while ischar(ln)...

Info iconThis preview shows pages 1–2. 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'); %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 = {};
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: %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; tcount = tcount + 1; [tok rest] = strtok(rest,','); end ln = fgetl(fh); count = count + 1; %adds one to the count (row) tcount = 1; %re-initializes our token count at 1 (columns) end fclose(fh); end ln = fgets(fh); %terminating condition if ischar(ln) %checks the terminating condition to continue the while loop count = count + 1; tcount = 0; rest = ln; %renames our ln variable while ~isempty(rest) %checks if the rest is an empty string after tokenizing [tok rest] = strtok(rest,','); if ~isempty(tok) tcount = tcount + 1; %adds one to the columns ca{count,tcount} = tok; %indexes the rows and columns and places the token into a %cell at that position end end end end fclose(fh); end e...
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 Institute of Technology.

Page1 / 2

LLcsv - %creates an empty cell array while ischar(ln)...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online