How to read pcap files directly using matlab
Show older comments
Hi,
I have to extract statistical features like source port, destination port, packet length, total length and round trip time from .pcap files by using Matlab. I tried some widespread ways I found on the Internet (pcap2matlab, traceplay) but I didn't make them work. Is there anyone who has a new suggestion? I will really appreciate your help.
Regards,
Asli
Accepted Answer
More Answers (1)
michael
on 20 Jun 2020
0 votes
To make the pcap2matlab litlle bit simple to use:
capture = pcap2matlab(filter, decodeas_and_dissector, filename_or_interface, capture_stop_criteria)
Lets say you are using a udp protocol and the data you would like to read is on top of it: data[0:1] (in packet showed below is 0x5d 0x2d)
Therfore in order to read a file you shall use:
- filter=[]; %there is no specific filter
- decodeas_and_dissector.somedata=base+0:base+1; (where base is the location of the 1st byte of the data (0x2a=42))
- capture_stop_criteria=[];
Now, lets assume that your data is dissected, like udp.srcport,
Therfore in order to read a file you may use:
- decodeas_and_dissector = {'udp.srcport'}
other option is like before
- decodeas_and_dissector.srcport = 34:35 %locations (0x22:0x23)

9 Comments
Bram Surewaard
on 23 Jul 2020
I tried to use your explanaition but i think i dont have a good enough understanding to make it work.
I have a pcap file that consists of different frames of sonar data, from which i want to use the range measurements in matlab. This is what the pcap file looks like:

I would like to work with the data from that is in Range Data and Angles (equi-angle) but I cannot figure out how to extract this data with the pcap2matlab command, could you maybe help me? It would be really nice if you could help me with this as I have been stuck on it for pretty long.
Thanks!
michael
on 24 Jul 2020
What the command you are issueing?
What are the parameters for this command?
Bram Surewaard
on 28 Jul 2020
Yeah that is the problem, i have no idea what kind of input would make sense and how I find this out.
I dont have the right knowledge of this kind of data and how to work with it.
Do you have any suggestions on what kind of commands I should use?
It would really help me out if you where able to help me!
These are the files I am working with:
Dissector https://gofile.io/d/bCXBDG
Huy Nguyen Duc
on 16 Feb 2022

Is there anyway to fix this issue?
Thank you, Huy Nguyen.
Neeraj Pal
on 22 Mar 2022
find and replace tshark -r with "c:\program files\wireshark\tshark" -r
Buns Cutie
on 2 Apr 2022
Edited: Buns Cutie
on 2 Apr 2022
hello, I'm a senior from China. I have met some problems when I used above code, like following picture. I want to read a wireshark packet(.pcapng) to analysis it's feature. How should I do? Thanks for everyone.

capture = pcap2matlab([],[],'gigE_image.pcapng',[]);
Started reading captured file:
Started importing to MATLAB:
错误使用 fieldnames
'double' 类型的输入参数无效。输入必须为结构体或者 Java 或 COM 对象。
出错 pcap2matlab (第 210 行)
FieldsofDissector = fieldnames(decodeas_and_dissector);
Marek HICAR
on 29 Nov 2022
Edited: Marek HICAR
on 29 Nov 2022
Got an error:
'tshark' is not recognized as an internal or external command,
operable program or batch file
pcap2matlab([], 'udp.srcport==2451', 'data.pcap', []);
Can you clarify?
Walter Roberson
on 29 Nov 2022
You need to install WireShark https://www.wireshark.org/download.html . The pcap2matlab() code invokes an executable named tshark from the wireshark installation.
Marek HICAR
on 22 Dec 2022
Thanks Walter, I do have WireShark installed before launching the pcap2matlab file.
The file was recordered. Are the input arguments correct? See attached fig.
Categories
Find more on Software Development Tools in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!