How to read a .txt file?

5 views (last 30 days)
Graur Alin
Graur Alin on 12 Jun 2015
Edited: Stephen23 on 12 Jun 2015
Is it possible to read this file line by line and create two matrices with two columns and a different number of lines?
The file looks like this and it's created with a function for storing some fingerprint data and associating it with a name and birth date:
And I want to create a matrix with the values of X and Y on two columns for both types.
Let me translate those lines:
Nume - Name;
Data - Birth Date;
Numar de sfarsituri de crestatura - Number of ridge endings;
Numar de bifurcatii - Number of bifurcations;
Sfarsituri de crestatura - Ridge Endings;
Bifurcatii - Bifurcations;
As I said, every ridge ending or bifurcation has two coordinates (they're in a matrix, of course). For example, lets say I want the ridge endings thingy. I need to read those lines and print a matrix like this: SFC = [84 32;78 60;131 105; ... ]
Is that possible?
  1 Comment
Stephen23
Stephen23 on 12 Jun 2015
Edited: Stephen23 on 12 Jun 2015
@Graur Alin: It makes our lives much easier if you give us text data as text, not as an image. Then we can actually use your text, instead of having to write it all out again.
You can either include it as code (formatted using the {} Code button), or upload it using the paperclip button (then pressing both Choose file and Attach file).

Sign in to comment.

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 12 Jun 2015
fid=fopen('fic.txt')
l=fgetl(fid)
k=1
while ischar(l)
r{k}=l
l=fgetl(fid)
k=k+1;
end
fclose(fid);
ii=find(~cellfun(@isempty,regexpi(r,'x\sy')));
idx=ii+1;
idy=[ii(2:end)-1 numel(r)];
M=regexpi(r(idx(1):idy(1)),'\d+','match');
jj=~cellfun(@isempty,M);
M=M(jj);
M=reshape(str2double([M{:}]),2,[])'
N=regexpi(r(idx(2):idy(2)),'\d+','match');
jj=~cellfun(@isempty,N);
N=N(jj);
N=reshape(str2double([N{:}]),2,[])'
  6 Comments
Graur Alin
Graur Alin on 12 Jun 2015
Thank you, it works!
Graur Alin
Graur Alin on 12 Jun 2015
One more question if you don't mind. I've got that function's input parameters as a file path and it returns me M and N and now I have to read multiple text files in a loop and compare them to my fingerprint image.
I use this first:
files = dir('*.txt');
for m=1:length(files)
[M,N] = pc_read (??);
What do I put there ("??") if my read file function's input looks like this:
function [M N] = pc_read(file)
fid = fopen(file);
Thank you!

Sign in to comment.

More Answers (1)

Stephen23
Stephen23 on 12 Jun 2015
Edited: Stephen23 on 12 Jun 2015
For faster and neater code you can use textscan, which also converts the strings to numeric values where appropriate. You can try something like this:
fid = fopen('test.txt','rt'); % specify the permission!
hdr = textscan(fid,'%s%s',4,'Delimiter',':','HeaderLines',1);
tx1 = textscan(fid,'%s',1,'Whitespace','-:\n');
xy1 = textscan(fid,'%f%f','MultipleDelimsAsOne',true,'HeaderLines',1);
tx2 = textscan(fid,'%s',1,'Whitespace','-:\n');
xy2 = textscan(fid,'%f%f','MultipleDelimsAsOne',true,'HeaderLines',1);
fclose(fid);
%
hdr = [hdr{:}];
tx1 = [tx1{:}];
tx2 = [tx2{:}];
xy1 = cell2mat(xy1);
xy2 = cell2mat(xy2);
And we get the following values:
>> hdr
hdr =
'Nume' 'Graur_Alin'
'Data' '01_06_1992'
'Numar de sfarsituri de crestatura' '8'
'Numar de bifurcatii' '10'
>> tx1
tx1 =
'Sfarsituri de crestatura '
>> xy1
xy1 =
84 31
78 60
131 105
112 130
142 150
105 154
80 160
130 168
>> tx2
tx2 =
'Bifurcatii '
>> xy2
xy2 =
87 88
72 92
64 94
45 96
105 102
61 140
92 140
104 142
To perform this in a loop you can use this:
files = dir('*.txt');
names = {files.name};
for k = =1:length(names)
file = names{k}
...code here
end
Or read more here:
Because there was no sample file uploaded with the original questrion I created my own, which you can find here:

Categories

Find more on Read, Write, and Modify Image 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!