How do I create cell/struct array with the specific lines taken from a text file?

1 view (last 30 days)
this is the code that i currently working with:
% open the file
fid = fopen('images_list1.txt');
% initialize a lineNumer
lineNum = 0;
% keep reading the file
while 1;
% get a line of text
tline = fgetl(fid); lineNum = lineNum + 1;
% exit if the line is empty
if tline == -1;
break;
end
% check modulus of lineNum for every 2nd and 11th line
if rem(lineNum,13) == 2;
tline_2nd = tline;
%disp(tline);
elseif rem(lineNum,13) == 11;
tline_11th = tline;
%disp(tline);
end
end
May I know how do I create cell/struct arrays with the every two lines I have extracted from my text file. For the first line, I'm extracting the image name with a format of "12345678.jpg" and for the second line, i'm extracting the no. of likes, which i would want it to be in a matrix if it is possible. Much help needed.. thanks!
  2 Comments
Stephen23
Stephen23 on 28 Oct 2015
Can you upload the data file please. It is useful for us to try code on. You can upload a file by clicking the paperclip button and then both the Choose file and Attach file buttons.

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 28 Oct 2015
Edited: Stephen23 on 28 Oct 2015
EDIT: See the improved code at the end on this answer
% read the file as a string:
str = fileread('images_list1.txt');
% define newline:
nwl = '[\n\r]{1,2}';
% split string into blocks: '#number\nfilename.ext\n...'
fmt1 = ['^#(\d+)',nwl,'(\d+\.[a-zA-Z]{1,4})',nwl,'(.+?)(?=^#)'];
C = regexp(str,fmt1,'lineanchors','tokens');
C = vertcat(C{:});
% convert block number to numeric:
N = cellfun(@str2double,C(:,1));
%find(diff(N)~=1) % to locate incorrectly parsed blocks.
% split all 'The XXX is:' substrings of the remaining text:
fmt2 = ['^The ([^\n]+) is:',nwl,'([^\n]+)'];
D = regexp(C(:,3),fmt2,'lineanchors','tokens');
D = vertcat(D{:})';
D = vertcat(D{:})';
D = reshape(D,[],size(C,1))';
% convert number of likes to numeric:
nLikes = cellfun(@str2double,D(:,8));
This code imports all of the data in your example file, and converts the "number of likes" into a numeric vector:
>> nLikes
nLikes =
25
56
9197
11559
4
8557
2417
13
22
2820
... etc
You can explore the rest of the data in the MATLAB workspace browser. Note that I made some assumptions about the data (e.g. that the filenames consist only of digits), as per your example file.
EDIT fixed bug to read last group within file:
% read file into string:
fid = fopen('images_list1.txt','rt');
str = fscanf(fid,'%c');
fclose(fid);
% break string into blocks:
fmt1 = '^#(\d+)\n(\w+\.\w{1,4})\n([^#]+)';
C = regexp(str,fmt1,'lineanchors','tokens');
C = vertcat(C{:});
% convert block numbers into numeric:
N = cellfun(@str2double,C(:,1));
%find(diff(N)~=1) % to locate incorrectly parsed blocks.
% split block data into fields:
fmt2 = '^The ([^\n]+) is:\n([^\n]+)';
D = regexp(C(:,3),fmt2,'lineanchors','tokens');
D = vertcat(D{:})';
D = vertcat(D{:})';
D = reshape(D,[],size(C,1))';
% convert number on likes into numeric:
nLikes = cellfun(@str2double,D(:,8));
  9 Comments
Stephen23
Stephen23 on 28 Oct 2015
Edited: Stephen23 on 28 Oct 2015
You are quite right that N does not contain the filenames. That is because N contains the block numbers of the file data. I gave you this data because it could be useful to check the file parsing, or for your data processing.
I never stated that N contains the filenames. However I referred to the filenames multiple times, and each time I told you that they are stored in C(:,2). I even printed C(:,2) in my command window and showed you this too.
Please look at C(:,2), not N.
Josephine Ng
Josephine Ng on 29 Oct 2015
Yes yes. So sorry. I thought N is where filenames will be contained. Sorry that i assumed.
May i ask another qn? If I want to load this matlab file inside another matlab file to associate the filenames, how should i use it? attached above(cnn_classify.m) is the other matlab file that i'm extracting features from images. what i need to achieve is the filenames of the extracted features associating with filenames from this text file.

Sign in to comment.

More Answers (0)

Categories

Find more on Convert Image Type in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!