How to read text file

Hi,
I have a text file, I want to store the following information:
Data1: Information specified below "Additional information":
1. Alarmtype1 heater volt:1670890
2. system error:5480
3. Alaram, voltage, event ID:46754
4. warning, id:15643
5. warning current, event id:76542
Data2: Information specified below "xy(24 points)":
-14 0.28
-13 0.27
-12 0.26
-11 0.25
-10 0.24
-9 0.23
-8 0.23
-7 0.22
-6 0.22
-5 0.22
-4 0.22
-3 0.22
-2 0.22
-1 0.22
0 0.23
1 0.22
2 0.22
3 0.22
4 0.22
5 0.22
6 0.22
7 0.23
8 0.23
9 0.23
10 0.24
11 0.26
12 0.29
Many many thanks in advance,

Answers (1)

fid=fopen('RainFallReport9.txt')
s=textscan(fid,'%s','delimiter','\n')
fclose(fid)
s=s{:}
idx=find(~cellfun(@isempty,regexpi(s,'additional information')))
b=s(idx:end)

8 Comments

Sir,
I want to store separatly that:
-14 0.28
-13 0.27
-12 0.26
-11 0.25
-10 0.24
-9 0.23
-8 0.23
-7 0.22
-6 0.22
-5 0.22
-4 0.22
-3 0.22
-2 0.22
-1 0.22
0 0.23
1 0.22
2 0.22
3 0.22
4 0.22
5 0.22
6 0.22
7 0.23
8 0.23
9 0.23
10 0.24
11 0.26
12 0.29
and in another file the below:
1670890
5480
46754
15643
76542
Sir, again kindly help.
fid=fopen('RainFallReport9.txt')
s=textscan(fid,'%s','delimiter','\n');
fclose(fid);
s=s{:};
idx=find(~cellfun(@isempty,regexpi(s,'additional information')));
b=s(idx:end);
idx=find(~cellfun(@isempty,regexp(b,'xy','match')));
a=regexp(b(idx+1:end-1),'[\.\d]+','match');
data1=str2double(reshape([a{:}],[],2));
c=b(3:7);
d=regexp(c,'\d+(?=\d)','match');
data2=str2double([d{:}])';
data1
data2
I still not getting the xy data as I want I am getting the data1 as below: data1 =
14.0000 0.2200
0.2800 0
13.0000 0.2300
0.2700 1.0000
12.0000 0.2200
0.2600 2.0000
11.0000 0.2200
0.2500 3.0000
10.0000 0.2200
0.2400 4.0000
9.0000 0.2200
0.2300 5.0000
8.0000 0.2200
0.2300 6.0000
7.0000 0.2200
0.2200 7.0000
6.0000 0.2300
0.2200 8.0000
5.0000 0.2300
0.2200 9.0000
4.0000 0.2300
0.2200 10.0000
3.0000 0.2400
0.2200 11.0000
2.0000 0.2600
0.2200 12.0000
1.0000 0.2900
But I want is as below:
-14 0.28
-13 0.27
-12 0.26
-11 0.25
-10 0.24
-9 0.23
-8 0.23
-7 0.22
-6 0.22
-5 0.22
-4 0.22
-3 0.22
-2 0.22
-1 0.22
0 0.23
1 0.22
2 0.22
3 0.22
4 0.22
5 0.22
6 0.22
7 0.23
8 0.23
9 0.23
10 0.24
11 0.26
12 0.29
Because I want to plot column1 vs column2 later.
Sir, sorry to bother you, please help me. Sincerely, Mekala
Change this line
a=regexp(b(idx+1:end-1),'[\.-\d]+','match')
I still get the answer as below:
-14 0.22
0.28 0
-13 0.23
0.27 1
-12 0.22
0.26 2
-11 0.22
0.25 3
-10 0.22
0.24 4
-9 0.22
0.23 5
-8 0.22
0.23 6
-7 0.22
0.22 7
-6 0.23
0.22 8
-5 0.23
0.22 9
-4 0.23
0.22 10
-3 0.24
0.22 11
-2 0.26
0.22 12
-1 0.29
But I really want as below:
-14 0.28
-13 0.27
-12 0.26
-11 0.25
-10 0.24
-9 0.23
-8 0.23
-7 0.22
-6 0.22
-5 0.22
-4 0.22
-3 0.22
-2 0.22
-1 0.22
0 0.23
1 0.22
2 0.22
3 0.22
4 0.22
5 0.22
6 0.22
7 0.23
8 0.23
9 0.23
10 0.24
11 0.26
12 0.29
Sincerely,
fid=fopen('RainFallReport9.txt')
s=textscan(fid,'%s','delimiter','\n');
fclose(fid);
s=s{:};
idx=find(~cellfun(@isempty,regexpi(s,'additional information')));
b=s(idx:end);
idx=find(~cellfun(@isempty,regexp(b,'xy','match')));
a=regexp(b(idx+1:end-1),'[-\.\d]+','match');
data1=str2double(reshape([a{:}],2,[])');
c=b(3:7);
d=regexp(c,'\d+(?=\d)','match');
data2=str2double([d{:}])';
data1
data2
Sir,
Thank you very much, but lastly I want to ask you about:
c=b(3:7);
Acually the below one is not a fixed one, but it will vary:
1. Alarmtype1 heater volt:1670890
2. system error:5480
3. Alaram, voltage, event ID:46754
4. warning, id:15643
5. warning current, event id:76542
I mean 1~5 will vary, may be some time 1~3, some time 1~4, or some time 1~8 or what ever. That is why instead I want to stop when dotted line appears (--------) for this Data2 storing.
One more thing, I have to read multiple text files from the directory at the same time. I attached herewith four text files for example.
May I humbly request you to kinldy help,
Sincerely,
Your's
Sir,
I want to read multiple files, please help me which part of the code should be changed. Sincerely, Mekala

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!