0
votes

I tried to import data from a text file. The data file format should be the first row are char and others are numbers, just like a xls file. But the file is generated by other software as a text file, where all data is in one row, with one space bar separated and 'tab' as a new row of data.

The data format I want to import into matlab should be like this:

A B C D
1 2 3 4
5 6 8 8
8 0 9 5

But now the data in the text file is

A B C D   1 2 3 4   5 6 8 8   8 0 9 5

How should I import the text file into the matlab in a matrix like please? I tried importdata but the ans is 4x1 cell. My Matlab version is 2008.

Sorry I am very new to matlab. Thank you.

Edited: I realized that the data is

A B C D  1 2 3 4  5 6 8 8 8  8 0 9 5  7 0 8 8 8  5 0 9 9 

Would you please teach me how to put these things into the cell, with one char/number one cell please?

   A B C D
   1 2 3 4
5  6 8 8 8
   8 0 9 5
7  0 8 8 8
   5 0 9 9 

Coz when i use cell2mat as suggested, error msg is CAT arguments dimensions are not consistent.

Thank you very much!

1
Because you have two different types (double and char), it's not possible to put everything in one matrix. That's the reason why ìmportdata produces a cell array. - bushmills
Why are you still using the 2008 version of Matlab? - Bernhard
Not paying for a license are we? =P - Stewie Griffin

1 Answers

1
votes

Let's start with the cell array you generated from the text file. I call it A. It is a 4x1. Or maybe 1x4? With the following line we make sure that we are starting from the same point:

A = A(:); % make sure A is columnar

A is a cell array with the first element containing the names and from 2nd to the end containing numbers as string.


Case 1: Consistent size of each section: Making a matrix

Here we will see how to extract your desired matrix from this cell array.

First, we need strread to make a vector from a string (You can also use textscan):

v = strread(A{2})

v =

     1     2     3     4

Now we should apply this function on each cell and combine the vectors in a matrix. That could be achieved using cellfun, but since we have to first produce a cell array, at the end we will need a cell2mat:

M = cell2mat(cellfun(@strread, A(2:end), 'UniformOutput', false))

Here, M is the final result.

M =

     1     2     3     4
     5     6     8     8
     8     0     9     5

Case 2: Inconsistency in the sizes + combining char / number: Making a cell array

We want to transform the data into a cell array with each element occupying one cell. Starting from first row of celldata we can break the string of names into a cell array using strsplit:

celldata = strsplit(A{1});

% maximum data count in each cell of A
maxL = max(cellfun(@(x) numel(strsplit(x)), A));

% constructing an empty cell array to be filled with the data
% the importance of this step dramatically increases with the data size! 
celldata{numel(A), maxL} = [];

Now you can go through celldata and fill the cells of it with the proper values:

for ii = 2:numel(A)
    val = strread(A{ii});
    celldata(ii, 1:numel(val)) = num2cell(val);
end

celldata is a cell array in this form:

celldata = 

    'A'    'B'    'C'    'D'     []
    [1]    [2]    [3]    [4]     []
    [5]    [6]    [8]    [8]    [8]
    [8]    [0]    [9]    [5]     []
    [7]    [0]    [8]    [8]    [8]
    [5]    [0]    [9]    [9]     []