How to apply conditional statements so as to select certain rows or columns on a given dataset.

How do I apply conditional statements so as to select certain rows or columns on a given dataset (like the one below)? For example, I would like to (1) display records with r only , (2) count records with + only .
Please note that the example of the data to be analyzed is given below. The data is separated by space. The data has 12 columns whose headings are; Event, time, from_Node, to_Node, Packet_Type, Packet_Size, Flags, Fid, Source_Address, Destination_Address, Sequence_Number and Packet_Id .
For example, + 0.155355 1 3 tcp 1500 ------- 0 0.0 3.0 2 3;
+ is an event,
0.155355 is time in secs,
1 is from_Node,
3 is to_Node,
tcp is Packet_Type, 1500 is Packet_Size,
------- are Flags,
0 is Fid,
0.0 is Source_Address,
3.0 is Destination_Address,
2 is Sequence_Number and
3 is Packet_Id.
_ _This is the data _
+ 0.155355 1 3 tcp 1500 ------- 0 0.0 3.0 2 3
- 0.160955 1 3 tcp 1500 ------- 0 0.0 3.0 2 3
r 0.170955 1 3 tcp 1500 ------- 0 0.0 3.0 1 2
r 0.178955 1 3 tcp 1500 ------- 0 0.0 3.0 2 3
+ 0.203632 1 3 tcp 1500 ------- 0 0.0 3.0 3 6
- 0.203632 1 3 tcp 1500 ------- 0 0.0 3.0 3 6
+ 0.206032 1 3 tcp 1500 ------- 0 0.0 3.0 4 7
+ 0.211632 1 3 tcp 1500 ------- 0 0.0 3.0 5 8
Thank you

 採用された回答

Cedric
Cedric 2014 年 7 月 3 日
編集済み: Cedric 2014 年 7 月 4 日
Here is another approach
content = fileread( 'myData.txt' ) ;
nPlus = sum( content == '+' ) ;
rRows = regexp( content, '(?<=(^|\n)r )[^\r\n]*', 'match' ) ;
with that you get
>> nPlus
nPlus =
4
and
>> fprintf( '%s\n', rRows{:} ) ;
0.170955 1 3 tcp 1500 ------- 0 0.0 3.0 1 2
0.178955 1 3 tcp 1500 ------- 0 0.0 3.0 2 3
PS: I can explain the regexp pattern if anyone is interested. Just let me know.
EDIT: I slightly modified the call to regexp to manage cases with/without header.

3 件のコメント

Sara
Sara 2014 年 7 月 3 日
If you are volunteering to explain regexp I'll listen, I haven't understood it enough to use it successfully yet!
Cedric
Cedric 2014 年 7 月 3 日
編集済み: Cedric 2014 年 7 月 3 日
The pattern matches the following
  • as many characters as possible which are different from \r and |
Manduna Watson
Manduna Watson 2014 年 7 月 21 日
Using the same technique, how can I, for example,
(A): select and display data with + , 3 , and tcp only .
(B): select and add data with + , 3 , tcp and 3.0 only

サインインしてコメントする。

その他の回答 (1 件)

Sara
Sara 2014 年 7 月 3 日
clc
clearvars
% Read file
filecontent = cell(1000,1);
k = 0;
fid = fopen('mydata.txt','r');
while 1
t = fgetl(fid);
if(~ischar(t)),break,end
k = k + 1;
filecontent{k} = t;
end
filecontent = filecontent(1:k);
fclose(fid);
% To get only the first character
fid = fopen('mydata.txt','r');
t = textscan(fid,'%s%*[^\n]');t = t{1};
fclose(fid);
% r data
index_r = find(strcmp(t,'r')==1);
filecontent(index_r)
% + data
index_plus = find(strcmp(t,'+')==1);
numel(index_plus)

5 件のコメント

Perfect solution. Thank you
I have been going through your perfect answer. I however had challenges of utilising the extracted data (filecontent). For example, how can I display and use the first (event) and second (time)column only ? The objective is to find the position where r appears , and then plot it against column 2 ).
For example; from the data above, r appears on position 3, 8, 17, 19 etc. The corresponding time values for r are 0.12028, 0.17096, 0.17896, 0.22163 .
In short, I want to have something like this;
event = [3,8, 17,19,20]
timeReceived =[0.12028, 0.17096, 0.17896, 0.22163, 0.22963]
Thank you
Unde % r data, try this and see if it does what you like. ps: 'r' appears only on line 3 and 4 for the data posted.
index_r = find(strcmp(t,'r')==1);
f = filecontent(index_r);
value = cellfun(@(t)sscanf(t,'%*s %f%*[^\n]',...
numel(index_r)),f,'UniformOutput',0);
value = cell2mat(value);
plot(index_r,value,'o')
Thank you, it works
Manduna Watson
Manduna Watson 2014 年 7 月 21 日
Using the same technique, how can I, for example,
(A): select and display data with + , 3 , and tcp only .
(B): select and add data with + , 3 , tcp and 3.0 only

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by