% Function Name: LLcsvread % Inputs (1): - (char) name of a csv file containing
ID: 3538023 • Letter: #
Question
% Function Name: LLcsvread
% Inputs (1): - (char) name of a csv file containing various data
% Outputs (1): - (cell) a cell array where each comma separated value
% is its own cell
%
% Function Description:
% 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 LLcsvread 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.
Explanation / Answer
function output = LLcsvread(file)
fid = fopen(file);
tline = fgetl(fid);
line=1;
output={};
while ischar(tline)
tmp=regexp(tline,',','split');
[r,c]=size(tmp);
output(line,1:c)=tmp;
line=line+1;
tline = fgetl(fid);
end
fclose(fid);
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.