how to extract a specific text from text file

I extract this line from my file : {'1;158.798;281.117;99.858;39.855;2182;'} and Next step is to extract the second , third , fourth and fifth numbers like this : [158.798,281.117,99.858,39.855]
Any help please ?

2 Comments

@sofa BY: your example also incudes the fifth number. Is this correct?

Sign in to comment.

 Accepted Answer

Stephen23
Stephen23 on 16 Aug 2018
Edited: Stephen23 on 16 Aug 2018
>> C = {'1;158.798;281.117;99.858;39.855;2182;'};
>> D = regexp(C,';','split');
>> D{1}(2:5)
ans =
'158.798' '281.117' '99.858' '39.855'
>> str2double(D{1}(2:5)) % convert to numeric
ans =
158.798 281.117 99.858 39.855

7 Comments

I need the fifth number also please ! I m sorry
Stephen23
Stephen23 on 16 Aug 2018
Edited: Stephen23 on 16 Aug 2018
@safa BY: Done. Check my edited answer. Surely you could have changed that 4 to a 5 yourself!
D{1}(2:5) It works for me now thank you I have an other question
[~, str]=system('tail -n 1 CC999-CORA-B04-192Data.csv')%
how can I change filename with a variable not a static filename like this ' CC999-CORA-B04-192Data.csv'
Use sprintf, e.g.:
fnm = 'CC999-CORA-B04-192Data.csv';
sprintf('tail -n 1 %s',fnm)
Thank you so much !!!!!!!!!!!!!
Hello again Stephen, Can you help me please , ? I need to add a header to my textfile and this is my code :
for k=1:nbrRect
Cellule{k}=get(rectangles(k), 'Position');
tag= CellTag(k);
x1 = Cellule{k}(1)+Cellule{k}(3)/2;
y1 = Cellule{k}(2)+Cellule{k}(4)/2;
fprintf(fileID,'%d;%5.3f;%5.3f;%5.3f;%5.3f;%d;%5.3f;%5.3f;\n',tag,Cellule{k}(1),Cellule{k}(2),Cellule{k}(3),Cellule{k}(4),numf,x1,y1);
end
I added this line before fopen and the boucle for but there is no header in my file:
fprintf(fileID,'%d;%5.3f;%5.3f;%5.3f;%5.3f;%d;;%5.3f;%5.3f;\n','Person ID','pos1','pos2','Width','height','X centre','Y centre');
Stephen23
Stephen23 on 17 Aug 2018
Edited: Stephen23 on 17 Aug 2018
"I added this line before fopen and the boucle for but there is no header in my file"
That line will need to go after fopen and before the loop. You will also need to change the all of the format specifiers to %s.

Sign in to comment.

More Answers (0)

Categories

Asked:

on 16 Aug 2018

Edited:

on 17 Aug 2018

Community Treasure Hunt

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

Start Hunting!