Extract specific values from a text file corresponding to a string

조회 수: 1 (최근 30일)
matlab newbie
matlab newbie 2018년 11월 29일
댓글: Adam Danz 2018년 11월 29일
I have the log file as stated below. I want to search for the occurences of the following line containing : imxv4l2videosrc0_src. And later extract the value `fps`, to plot them as a graph. Can anyone provide some pointers on how to do this?
0:00:02.249654647 [332m 1465[00m 0x19cfce0 [37mTRACE [00m [00;34m GST_TRACER :0::[00m framerate, pad=(string)pay0_src, fps=(uint)44;
0:00:02.249798648 [332m 1465[00m 0x19cfce0 [37mTRACE [00m [00;34m GST_TRACER :0::[00m framerate, pad=(string)imxv4l2videosrc0_src, fps=(uint)22;
0:00:02.249937982 [332m 1465[00m 0x19cfce0 [37mTRACE [00m [00;34m GST_TRACER :0::[00m framerate, pad=(string)h264parse0_src, fps=(uint)21;
0:00:02.250077650 [332m 1465[00m 0x19cfce0 [37mTRACE [00m [00;34m GST_TRACER :0::[00m framerate, pad=(string)send_rtp_sink_0_proxypad1, fps=(uint)47;
0:00:02.250217318 [332m 1465[00m 0x19cfce0 [37mTRACE [00m [00;34m GST_TRACER :0::[00m framerate, pad=(string)rtpbin_send_rtp_src_0, fps=(uint)47;
0:00:02.250353319 [332m 1465[00m 0x19cfce0 [37mTRACE [00m [00;34m GST_TRACER :0::[00m framerate, pad=(string)imxvpuencoderh264_0_src, fps=(uint)21;
0:00:02.250492320 [332m 1465[00m 0x19cfce0 [37mTRACE [00m [00;34m GST_TRACER :0::[00m framerate, pad=(string)queue1_src, fps=(uint)44;
0:00:02.250631321 [332m 1465[00m 0x19cfce0 [37mTRACE [00m [00;34m GST_TRACER :0::[00m framerate, pad=(string)queue0_src, fps=(uint)22;
0:00:03.249094861 [332m 1465[00m 0x19cfce0 [37mTRACE [00m [00;34m GST_TRACER :0::[00m framerate, pad=(string)rtpsession0_send_rtp_src, fps=(uint)59;
0:00:03.249463531 [332m 1465[00m 0x19cfce0 [37mTRACE [00m [00;34m GST_TRACER :0::[00m framerate, pad=(string)queue2_src, fps=(uint)59;
0:00:03.249620532 [332m 1465[00m 0x19cfce0 [37mTRACE [00m [00;34m GST_TRACER :0::[00m framerate, pad=(string)pay0_src, fps=(uint)60;
0:00:03.249761533 [332m 1465[00m 0x19cfce0 [37mTRACE [00m [00;34m GST_TRACER :0::[00m framerate, pad=(string)recv_rtcp_sink_0_proxypad3, fps=(uint)1;
0:00:03.249901201 [332m 1465[00m 0x19cfce0 [37mTRACE [00m [00;34m GST_TRACER :0::[00m framerate, pad=(string)imxv4l2videosrc0_src, fps=(uint)27;
0:00:03.250039202 [332m 1465[00m 0x19cfce0 [37mTRACE [00m [00;34m GST_TRACER :0::[00m framerate, pad=(string)h264parse0_src, fps=(uint)28;
0:00:03.250175869 [332m 1465[00m 0x19cfce0 [37mTRACE [00m [00;34m GST_TRACER :0::[00m framerate, pad=(string)send_rtp_sink_0_proxypad1, fps=(uint)59;
0:00:03.250314537 [332m 1465[00m 0x19cfce0 [37mTRACE [00m [00;34m GST_TRACER :0::[00m framerate, pad=(string)rtpbin_send_rtp_src_0, fps=(uint)59;
0:00:03.250451538 [332m 1465[00m 0x19cfce0 [37mTRACE [00m [00;34m GST_TRACER :0::[00m framerate, pad=(string)imxvpuencoderh264_0_src, fps=(uint)28;
0:00:03.250587873 [332m 1465[00m 0x19cfce0 [37mTRACE [00m [00;34m GST_TRACER :0::[00m framerate, pad=(string)queue1_src, fps=(uint)60;

답변 (1개)

Adam Danz
Adam Danz 2018년 11월 29일
편집: Adam Danz 2018년 11월 29일
Here's one approach.
"I want to search for the occurences of the following line containing : imxv4l2videosrc0_src".
1. Load your log file into matlab using one of the methods in the link. Perhaps textscan() is best. Given your task, it would be easiest if your data were organized in a cell array where each element is a line in your log file but there are lots of alternatives to how you can organize the data.
2. Assuming the data are organized by row, you can use regexp() , strfind() , or contains() to search for which rows have the substring "imxv4l2videosrc0_src".
3. You want an index of elements that contain this substring. The contains function gives this to you directly. The regexp and strfind functions will return empties within a cell array if an element does not contain the search phrase. So, to identify which elements do contain the phrase, use this (below) where 'output' is the output of the regexp or strfind function and 'containsPhrase' is a logical vector identifying which elements contain the phrase.
containsPhrase = ~cellfun(@isempty, output);
"And later extract the value `fps`, to plot them as a graph"
I have no idea which column in your data are fps. The section above will give you the relevant rows of your log file to extract from. There are lots of ways to pull out a section of each row - especially if the rows have the same format. You'll likely use regexp() again but there might be even easier methods depending on what fps is.
  댓글 수: 2
matlab newbie
matlab newbie 2018년 11월 29일
So I did the following :
C = textscan(fid, '%s','delimiter', '\n')
Cd = cellstr(C{1,1})
idx = find(contains(Cd,'imxv4l2videosrc0_src'))
And now I have idx as a array of double with indexes. The fps coloumn is 158, and I could extract the values using :
extractAfter(Cd{idx},158)
However, the above line fails since the iteration throughout the idx array is not possible.
Is there a way to get through this?
Adam Danz
Adam Danz 2018년 11월 29일
It's really hard for me to imagine what your variables are here. What is Cd? Could you give me an example of what the variable looks like?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by