Need help in file processing

2 views (last 30 days)
Utpal Bakshi
Utpal Bakshi on 1 Feb 2013
Hi all, I need help on basic file processing : I have to convert a file format, the program should read a input file and after processing (which counts the number of each term in each group, see below), yield a output file. Here is the format of the input file:
Group1: somo|112345478 somo|734567233 homo|233689876
Group2: somo|904686712 somo|891145662 somo|106736432
Group3: aomo|397634567 aomo|123446789
Group4: aomo|905672345 aomo|120846789
........
..
From the above input, the output file should look like (the numbers after the pipe should not be considered):
somo homo aomo
Group1 2 1 0
Group2 3 0 0
Group3 0 0 2
Group4 0 0 2
I get stuck on this file conversion for sometime now and unable to figure it out. Any guidance is highly appreciated. Thanks in advance.. Apologies if this post doesn't belong here...
  2 Comments
Andreas Goser
Andreas Goser on 1 Feb 2013
The post belongs here, but it is unclear where you are stuck. Lets start with the import. Are you able to import the data and if not what is the current code and result.
Jan
Jan on 1 Feb 2013
The output table is not clear: It looks nice, but how do you want te represent this as Matlab variables? Is this a cell with 4 columns, of a struct vector with the fields GroupName, somo, aomo and homo?

Sign in to comment.

Answers (1)

Jan
Jan on 1 Feb 2013
Edited: Jan on 1 Feb 2013
fid = fopen(FileName, 'r');
if fid == -1, error('Cannot open file for reading'), end
Data = cell(1000, 2); % Guessed pre-allocation
iData = 0;
while true
s = fgetl(fid);
if ~ischar(s)
break;
end
iData = iData + 1;
Data{iData, 1} = strtok(s, ':');
Data{iData, 2} = [length(strfind(s, 'somo'), ...
length(strfind(s, 'homo'), ...
length(strfind(s, 'aomo')];
end
fclose(fid);
Data = Data(1:iData, :); % Crop undefined rows

Community Treasure Hunt

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

Start Hunting!