Read some data from a file containing numbers and characters

8 次查看(过去 30 天)
Hi, my problem is like this : I want to read file dynalog (.dyn) which contain text and number.
{
** D Y N A M I C B E A M S T A T I S T I C S **
TOTAL DOSE DELIVERED : 228 (MU)
DOSE STANDARD DEVIATION : 0.04 (MU)
DOSE-POSITION STANDARD DEVIATION : 0.38 (deg)
NUMBER OF SAMPLES : 1491
-- STT --
INSTANCE# DOSE GANTRY ANGLE (Internal scale)
(MU) (deg)
1 0.00 0.50
2 1.45 1.50
3 4.36 3.60
4 7.27 5.60
. . .
. . .
176 225.73 356.40
177 227.24 358.50
178 228.00 359.50
-- SEGMENT BOUNDARY SAMPLES (ACTUAL) --
INSTANCE# DOSE GANTRY ANGLE (Internal scale)
(MU) (deg)
1 0.00 0.50
2 1.47 1.23
3 4.37 3.28
4 7.29 5.28
5 10.20 7.30
....
..... }
my objective is to read data after :
-- SEGMENT BOUNDARY SAMPLES (ACTUAL) --
INSTANCE# DOSE GANTRY ANGLE (Internal scale)
(MU) (deg)
Any help -:)) please
  6 个评论
addou abdeljalil
addou abdeljalil 2015-7-13
I tried the proposition of Azzi Abdelmalek it work well for filename. txt not . dyn. i do not know what to do because the machine generates DYNALOG files. I think of a conversion .text before my calculations.
addou abdeljalil
addou abdeljalil 2015-7-16
Can you explain me what means this character :
idx = regexpi(str,'^\s+\--(\s[\w()]+)+\s--\s*$','LineAnchors');
Thank you

请先登录,再进行评论。

采纳的回答

Azzi Abdelmalek
Azzi Abdelmalek 2015-7-13
p='SEGMENT BOUNDARY SAMPLES'
fid=fopen('file.txt');
a=fgetl(fid);
test=isempty(regexp(a,p));
while ischar(a) & test==1
a=fgetl(fid)
test=isempty(regexp(a,p));
end
fgetl(fid);
fgetl(fid);
k=1;
a=fgetl(fid);
while ischar(a);
r{k,1}=a;
a=fgetl(fid);
k=k+1;
end
fclose(fid)
r
  2 个评论
addou abdeljalil
addou abdeljalil 2015-7-13
编辑:Azzi Abdelmalek 2015-7-13
I thank you Azzi. i try your proposition but i received this error : Error using fgetl (line 44) Invalid file identifier. Use fopen to generate a valid file identifier.
Error in TestReadLineFile (line 17) a=fgetl(fid);
code like this :
if true
p = 'SEGMENT BOUNDARY SAMPLES'
fid=fopen('G:\RADIOPHY\Stages PHYS\Stage Jalil 2015\Ex BANNE\BANNE-20130801-1\2015-02-04 134243 228.dyn', 'r');
a=fgetl(fid);
test=isempty(regexp(a,p));
while ischar(a) & test==1
a=fgetl(fid)
test=isempty(regexp(a,p));
end
fgetl(fid);
fgetl(fid);
k=1;
a=fgetl(fid);
while ischar(a);
r{k,1}=a;
a=fgetl(fid);
k=k+1;
end
fclose(fid)
r
end
i think the problem comes from the way (path) of my file.
addou abdeljalil
addou abdeljalil 2015-7-13
编辑:addou abdeljalil 2015-7-13
When i execut my code i have
r =
' 1 0.00 0.50'
' 2 1.47 1.23'
' 3 4.37 3.28'
.......
' 89 124.12 178.63'
' 90 125.11 180 .65'
' 91 126.11 182.75'
' 92 127.13 184.75'
.....
My last question is when i have 180 in the 3'th colum i will do some caclul.
if angle(i) <= 180
a_conv(i) = abs(angle(i) - 180) ;
else
a_conv(i) = abs(360 - (angle(i) - 180));
end
i tried to creat file and put *r* result in but i have some pbs. i used this code :
mat_R = fopen([CheminRes 'search180.txt'],'w');
if exist('search180.txt', 'file')==2
delete('search180.txt');
end
x = num2str(r) ;
fprintf(mat_R,'%f\n',r);
fclose(mat_R) ;

请先登录,再进行评论。

更多回答(2 个)

Stephen23
Stephen23 2015-7-13
编辑:Stephen23 2015-7-16
Of course you can use textscan, and this is likely to be the fastest and easiest way of obtaining the file data. Here is how you can get the header data, the STT data, and the Segment Boundary Samples data:
str = fileread('temp.txt');
idx = regexpi(str,'^\s+--(\s[\w()]+)+\s--\s*$','LineAnchors');
hdr = textscan(str(1:idx(1)),'%s%f%s','HeaderLines',3,'Delimiter',{':','(',')'})
stt = textscan(str(idx(1):idx(2)),'%f%f%f','HeaderLines',4)
sbs = textscan(str(idx(2):end),'%f%f%f','HeaderLines',4)
which we can check in the command window:
hdr =
{4x1 cell} [4x1 double] {4x1 cell}
stt =
[7x1 double] [7x1 double] [7x1 double]
sbs =
[5x1 double] [5x1 double] [5x1 double]
>> hdr{1}
ans =
'TOTAL DOSE DELIVERED '
'DOSE STANDARD DEVIATION '
'DOSE-POSITION STANDARD DEVIATION '
'NUMBER OF SAMPLES '
>> stt{2}
ans =
0
1.4500
4.3600
7.2700
225.7300
227.2400
228.0000
>> sbs{3}
ans =
0.5000
1.2300
3.2800
5.2800
7.3000
Because you did not upload a sample file I used this one:
  3 个评论
Stephen23
Stephen23 2015-7-16
编辑:Stephen23 2015-7-16
@addou abdeljalil: I believe this email is from you:
"Hi, I will come to you after your proposal for my matlab code.i use this code because it work well. In one line you wrote this form of code :
idx = regexpi(str,'^\s+--(\s[\w()]+)+\s--\s*$','LineAnchors');
But i do not understand all the symbols.
can you give me one explication please."
Lets have a look at the parts of this regular expression:
^ - start of line
\s+ - space character, >= 1 times
-- - two hyphens
( - start group
\s - space character, once
[\w()]+ - word or (), >= 1 times
)+ - end group, >=1 times
\s+ - space character, >=1 times
-- - two hyphens
\s* - any space characters
$ - end of the line
You will find the regular expressions documentation useful to read. You also might like to try using my FEX submssion RegexpHelper, which lets you interactively develop regular expressions:

请先登录,再进行评论。


Sid
Sid 2015-7-13
Can you use the Import tool ( MATLAB Doc, ) and import the data in as a cell array? You can also import headers separately, and the numeric data as a separate array as well.
Additionally, you can use textscan ( MATLAB Doc. )
  1 个评论
addou abdeljalil
addou abdeljalil 2015-7-13
Thanks for your answer. i can't use textscan because i don't have same number of column. my goal is to learn my data after detecting the line quoted beolw. I would have after several files that I have to look all the time the same thing so i can't use also import data.
Other suggestion will be very appreciated.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Import and Export 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by