analyse txt file with rows of different length

Hello, I have a data file input.txt which looks like this (simplified)
hello world
the sun
i like the sun
sun world world
what I want is to read this file, count the distinct words and make a matrix of it, which has the form
word1 word2 word3 ... wordn
example1 * * * ... *
example2 * * * ... *
...
examplem * * * ... *
where the * represents how many times a word is in that specific example. In this case the matrix will look like this:
1 1 0 0 0 0
0 0 1 1 0 0
0 0 1 1 1 1
0 2 0 1 0 0
I have tried this with textscan, but I get stuck with the cell arrays. I hope someone can help me with this..

 Accepted Answer

Answering is easier, if you ask a specific question. It is not clear, what kind of assistence you need.
FID = fopen(FileName, 'r');
if FID < 0, error('Cannot open file'); end
Data = textscan(FID, '%s', 'Delimiter', '\n');
Lines = Data{1};
% Now [Lines] is a cell string containing the lines
LongLine = sprintf('%s ', Lines{:});
Data = textscan(LongLine, '%s', 'Delimiter', ' ');
Words = Data{1};
Now I do not understand what "example1".."examplem" means. If these are the lines of the file:
% EDITED: Count the number of occurences
M = zeros(length(Words), length(Lines));
for i = 1:m
Data = textscan(Lines{i}, '%s', 'Delimiter', ' ');
lineWords = Data{1};
M(:, i) = cellfun(@(x) sum(strcmp(x, LineWords)), Words);
end
M = transpose(M);

2 Comments

Hi, thanks for your fast reply!
example1, example2, etc. are indeed the lines in my txt file, I should have named these line1, line2, etc.
The program works great now, only one thing left I need to do and that is to count the number of times a word appears in a line. Do you know a fast way to do this?
I've inserted the counting. I assume, you need a UNIQUE for the Words also.

Sign in to comment.

More Answers (0)

Categories

Products

Asked:

on 12 Apr 2011

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!