I want to read only the numbers from the text file between a range ( were the start and end differs constantly from one txt file to another)

4 views (last 30 days)
I want to read only the numbers from a txt file ( attached) by skipping the first few lines which are not needed till the end of the file(always skipping the last two lines)
for example
We read the first letter of every line in the file.
if the line starts with an alphabet i want it to be skipped and read it when it has a number as it's first letter.
So that eventually i will automatically get the range of the lines i want ,( from were the values start and end) automatically.
  6 Comments
Vinit
Vinit on 30 Jul 2014
I have the cell with all the values thanks to you....the problem i have now... the data is like this .......0 1 2 3 0 1 2 3 0 1 2 3.. these are redings of time... how to i combine the times so that when i plot all of this will be of a same line........?
dpb
dpb on 30 Jul 2014
Me no follow -- once you have the desired lines parsed, then just convert to numeric --
v=str2num(char(file));
Now you've got a Nx3 array of the values to do whatever you wish with.

Sign in to comment.

Accepted Answer

dpb
dpb on 30 Jul 2014
Complete comments in one place...
Or, simply read the full file in as text and eliminate the lines unwanted in memory. May well be faster overall than fgetl reading/parsing line-by-line.
file=textread('yourfile.txt','%s','delimiter','\n','whitespace','');
gives a cell array, one element/line. To eliminate the lines w/ initial alphabetic characters leaving the numeric ones,
isdigit=isstrprop(char(file),'digit'); isdigit=isdigit(:,1);
file=file(isdigit);
Used the intermediary logical array since can't use subscripting on expression results, unfortunately. Can do same operation w/ cellfun if prefer that route.
v=str2num(char(file));
Now you've got a Nx3 array of the values to do whatever you wish with.

More Answers (0)

Categories

Find more on Data Import and Export 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!