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 Kommentar

Stephen23
Stephen23 am 12 Jun. 2015
Bearbeitet: Stephen23 am 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).

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Azzi Abdelmalek
Azzi Abdelmalek am 12 Jun. 2015

1 Stimme

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 Kommentare

Graur Alin
Graur Alin am 12 Jun. 2015
Bearbeitet: Graur Alin am 12 Jun. 2015
r looks ok. The conversion it's troublesome:
Columns 1 through 4
[1x68 char] 'Nume: Graur_Alin ' 'Data: 01_06_1992 ' [1x38 char]
Columns 5 through 9
[1x24 char] [1x68 char] [1x68 char] [1x27 char] [1x68 char]
Columns 10 through 14
' X Y ' ' 84 31' ' 78 60' '131 105' '112 130'
Columns 15 through 19
'142 150' '105 154' ' 80 160' '130 168' [1x68 char]
Columns 20 through 24
'Bifurcatii : ' [1x68 char] ' X Y ' ' 87 88' ' 72 92'
Columns 25 through 29
' 64 94' ' 45 96' '105 102' ' 61 140' ' 92 141'
Columns 30 through 32
'104 142' '130 143' '109 147'
It works, somehow.
I get this error though:
f_read
Index exceeds matrix dimensions.
Error in f_read (line 13)
M = regexpi(r(idx(1):idy(1)),'\d+','match');
Azzi Abdelmalek
Azzi Abdelmalek am 12 Jun. 2015
attach the text file you want to read
Graur Alin
Graur Alin am 12 Jun. 2015
Bearbeitet: Graur Alin am 12 Jun. 2015
The thing is I have no experience with text files in MatLab at all.
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,'\sx\s+y')))
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,[])'
Graur Alin
Graur Alin am 12 Jun. 2015
Thank you, it works!
Graur Alin
Graur Alin am 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!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Stephen23
Stephen23 am 12 Jun. 2015
Bearbeitet: Stephen23 am 12 Jun. 2015

0 Stimmen

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:

Community Treasure Hunt

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

Start Hunting!

Translated by