Help modifying matlab script for text file

3 views (last 30 days)
I am trying to parse a text file that was made using Iperf in the command prompt window. I used the import function of matlab with custom delimiters to separate the data accordingly. However, because the window starts out with data in Kbits/sec and then ends in Mbits/sec, some of the numbers generated by the script are 1.03 etc when they should really be 1030. I'm not sure what I would need to add to the script to allow this to happen. I have attached the txt file as well as the script that was generated by matlab when I ran the import function. Thank you for any help that can be provided.

Accepted Answer

Matt Tearle
Matt Tearle on 13 Feb 2014
Edited: Matt Tearle on 13 Feb 2014
How about this:
filename = 'C:\Users\jmille76\Documents\MNVR\20140206\wnw\R2_10k_b1M_l1400.txt';
fileID = fopen(filename,'r');
data = textscan(fileID,'%*s%f %c%[^\n\r]','Delimiter',{'bits/sec','Bytes'});
R210kb1Ml1400 = data{1}.*1000.^(data{2}=='M');
fclose(fileID);
clearvars filename data fileID ans
Basically, it's keeping the first character of the "[K/M]bits/sec" column (using just the "bits/sec" as the delimiter), so it reads the column of numbers then a column of single characters which are either K or M. Where that second column is M, it multiplies the first column by 1000; where it's not, it multiplies by 1.
EDIT TO ADD: To make this more extensible, let's try a slightly different approach:
filename = 'C:\Users\jmille76\Documents\MNVR\20140206\wnw\R2_10k_b1M_l1400.txt';
% Read whole file as text
txt = fileread(filename);
% Clean up formatting!
% Remove spaces between - and a number (so "x- y" becomes "x-y")
txt = regexprep(txt,'- (\d)','-$1');
% Remove brackets and spaces around number in first column
txt = regexprep(txt,'\[\s+(\d)\]','$1');
% Read text as (properly) formatted data
% 3 0.0-1.0 sec 2.73 KBytes 22.4 Kbits/sec 34.313 ms 2/ 4 (50%)
% %f %f-%f %s %f %s %f %s %f %s %f/ %f (%f[text crap]
data = textscan(txt,'%f%f-%f%*s%f%s%f%s%f%s%f/%f(%f%*s');
% Extract data
R210kb1Ml1400 = data{6};
% Extract corresponding units
units = data{7};
% If units start with "M" instead of "K", multiply by 1000
R210kb1Ml1400 = R210kb1Ml1400.*1000.^strncmp('M',data{7},1);
This is using a cunning trick with textscan, which is that it can accept a string as input, rather than a file. So I'm using fileread to read the file as text and do some pre-formatting with regexp. (If you don't know regular expressions, this is magic -- just read the comments and be happy.)
Now data contains pretty much everything. You can choose what you do and don't want. If you want to ignore a column, put a * between the % and the s or f. For example, I'm ignoring the fourth column, which is always the string "sec". If you want that, remove the * in the format specifier. I've tried to show the breakdown of the format specifier in the comments, under the example line from the file.
To extract the data from the kth column, do variablename = data{k};, just like I did with R210kb1Ml1400 and units.
HTH.
  3 Comments
Matt Tearle
Matt Tearle on 13 Feb 2014
Unfortunately, this isn't a MATLAB-specific issue -- it's just the pain-in-the-ass that is file I/O, when the file doesn't have a very strict format. Humans are good at seeing the pattern of a file like this -- computers not so much. So trying to say "read the column of numbers at the end inside the parentheses" is difficult.
Pondering on it a bit, though, I think I can make it slightly easier for you... See my edit to my answer.
Josh
Josh on 17 Feb 2014
This was perfect and helped me understand to make some of my own tweaks. Thanks for the help Matt.

Sign in to comment.

More Answers (0)

Categories

Find more on Programming 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!