MATLAB: How to parse and write it to any document as same column but every time next row of .txt file

algorithmfunctionMATLABparse

Hi,
I have data file in .txt format. I need specific part of this data namely "idle" value.
[2020-07-09 15:53:40] version: 3
[2020-07-09 15:53:40] chanspec tx inbss obss nocat nopkt doze txop goodtx badtx glitch badplcp knoise idle timestamp
[2020-07-09 15:53:40] 0x1803 4 0 0 0 1 0 95 0 0 7 0 -97 99 222456
For instance, in above i need to parse idle value that is 99 and write it to any document like txt. But in my data file there are 600 times above iteration lines. So I need to 600 times parse and write "idle" value. Could anyone help me?
I added my data file(channel.txt).

Best Answer

FID = fopen('Full path to the file', 'r'); % i.e. C:\...\channel.txt
Counter = 1;
tline = fgetl(FID);
File_Data{Counter} = tline;
while ischar(tline)
Counter = Counter+1;
tline = fgetl(FID);
File_Data{Counter} = tline;
end
fclose(FID);
File_Data = File_Data';
This part of the code will read the file into variable File_Data.
Example of processing data for a line that contains the data you need, in this case third line.
Idle = textscan (File_Data{3,1}, '%s %s %s %d %d %d %d %d %d %d %d %d %d %d %d %d %d')
The value you're looking for is located on second to last index of the variable Idle. You can save it to a variable
Idle(1,16)
Write a for loop with textscan function for the rows (every third) that contain the value and save it in an array. Lastly, save an array to the file.
Hope this is clear enough.