2

I'm trying to read a .txt file that is ';' delimited with date in the 'header' and diferent columns after the 'header'. I'm using quotes to HEADER because it's more like a parameter line.

So, the .txt is like (the other lines have the same number of columns):

15/07/2013;66;157 
DDD;3;1;0;1;1;1;-0.565
DDD;8;2;0;2;1;1;-0.345 
DDD;9;3;2;3;1;2;-0.643 
DDD;8;1;3;5;1;3;-0.025 
DDD;8;1;0;9;1;4;-0.411 
DDD;15;1;5;4;1;5;-0.09 
DDD;12;1;0;5;1;6;-0.445 
DDD;13;1;0;7;1;7;-0.064

I want to read and create a matrix, that contains each data in one cell, like:

matrix = 
[15/07/2013 66 157
 DDD 3 1 0 1 1 1 -0,565
 DDD 8 2 0 2 1 1 -0,345
 DDD 9 3 2 3 1 2 -0,643
...]

I've tried textscan, cvsread, textread and nothing works!

Thanks in advance!

Edit: Actually, I found a WAY FASTER code to do this!

4
  • Well, if you think so, I'm sorry. I consider that different for some aspects.
    – Luiz
    Jul 15, 2013 at 17:21
  • That's my opinion, this question is very related, and the solution is practically the same.
    – Eitan T
    Jul 15, 2013 at 17:23
  • I got it. Thanks for your reply. I will pay more attention on it. Actually, can you tell me how do you find similar questions?
    – Luiz
    Jul 15, 2013 at 17:31
  • So, what should I do? Delete my question?
    – Luiz
    Jul 16, 2013 at 17:31

1 Answer 1

2

From my past experience, MATLAB does not like strings and numbers to be in the same matrix, so you would be forced to use a cell.

You can do this relatively easily with some simple paring.

fid = fopen('temp.txt','r'); %# open file for reading
count = 1;
content = {};
while ~feof(fid)
    line = strtrim(fgets(fid)); %# read line by line
       parts = regexp(line,';','split');
       for i = 1:numel(parts)
             temp = regexp(parts{i},'-?[0-9]*\.?[0-9]*(i|j)?','match');
             if numel(temp) >= 1 && strcmpi(temp{1},parts{i})
                  parts{i} = str2double(parts{i}) ;
             end
       end
       content{count} = parts;
    count = count + 1;
end
fclose(fid);

numRows  = size(content,2)-1;
whole = cell(numRows,8);
for i = 1:numRows
    for j = 1:8
       whole{i,j} = content{i+1}{j};
    end
end
content = {content{1},whole};

UPDATE

I added some stuff to put everything into a single cell array, all of the data outside of the header. I do not know if you wand the header to also be in that 8 column array, but if you do here is some code to do that

numRows  = size(content,2);
whole = cell(numRows,8);
for i = 1:numRows
    for j = 1:min([size(content{i},2),8])
       whole{i,j} = content{i}{j};
    end
end
whole
17
  • Wow, I would never think in this! Testing right now. Thanks for your reply!
    – Luiz
    Jul 15, 2013 at 16:37
  • it should work, I tested it on my end by just copying and pasting the text you posted above. Just remember, when accessing cells you need to use braces, {}, not parentheses, (). Jul 15, 2013 at 16:43
  • Thanks a lot, it worked. The problem is, the resultant is a CELL OF CELLS, and I need a cell with 8 columns and all rows. Is there any way to transform like this?
    – Luiz
    Jul 15, 2013 at 16:43
  • Yes, like I said, after the 'header' I will always have the same number of columns (8, in this case)
    – Luiz
    Jul 15, 2013 at 16:44
  • 1
    Take your time, and thanks a lot in advance, you are being very helpful! To discover the amount of rows u can see the "count" variable, actually, is like total_rows = count - 1. Is this helpful?
    – Luiz
    Jul 15, 2013 at 16:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.