0
votes

I am trying to convert a text file (comma seperated values) to a matrix in MATLAB. I have been able to fetch the individual lines into cell array but I can't convert those values to a matrix form. The 7X1 cell obtained by reading the line of the file is a row major and has the following values

line = 
      '123'
      ''
      '"Foo"'
      [1X27 char]
      '1.01'
      '2.02'
      '0'

So, the file is basically a collection of similar lines. Any suggestion as to how can I convert these lines to a matrix form ? Any help would be appreciated.

1
And yes, I would like the output to be of the format row=[123 Foo [1X27 char] 1.01 2.02 0]BajajG

1 Answers

0
votes

You can use the cell2mat function to create a matrix from your cell (see MATLAB help page for details). As your cell is a column vector and you want a row vector, you will have to transpose the cell first.

row = cell2mat(line.');

If you want to add a space between all elements, here's a way to do this:

% Remove all empty fields
line = line(~cellfun(@isempty,line));

% Add a space after each element
line = strcat(line,{' '});

% Now call cell2mat
row = cell2mat(line.');

% and remove space at end
row = strtrim(row);

You can use regexpi to find quotation marks and remove them.

row = row(regexpi(row,'"')) = '';