Extracting data from text file.

2 views (last 30 days)
Neal
Neal on 6 Sep 2016
Commented: D. Ali on 28 Apr 2019
I have several text files obtained from a measurement equipment.
The text file consists of both numbers and text.(First 20 rows and last 20 rows are text and number.)The data(2 columns) that needs to be used for data plot lies in the middle(7400 rows). Since, the file has both number and text. I used
Data=readtable('001.txt') %%filename is 001.txt
M=data(21:7421,:)% M is a table with the XY data to make graph.
However, the data is imported as character (numbers lie inside quotes, for e.g '2130'). How can the data be imported such that only numbers can be extracted? [I want the table to contain only numerical values. 2130 instead of '2130' and so on]
Are there other efficient ways of extracting XY data? Ultimately, I want to be able to read several .txt file and plot XY data. How can a loop be made if all files start with digit 0(for eg. *001_Newyork.txt,*002_Dallas.txt* and so on...)?
  2 Comments
Guillaume
Guillaume on 6 Sep 2016
Attach an example file.
Neal
Neal on 7 Sep 2016
The file identical to my problem

Sign in to comment.

Answers (3)

Takuya Otani
Takuya Otani on 7 Sep 2016
In order to specify data type on each column, use 'Format' option in readtable function.
You could try something like, following (copied from the doc page from readtable)
T = readtable('myCsvTable.dat','Format','%s%s%u%f%f%s')
T =
LastName Gender Age Height Weight Smoker
__________ ______ ___ ______ ______ ______
'Smith' 'M' 38 71 176 '1'
'Johnson' 'M' 43 69 163 '0'
'Williams' 'F' 38 64 131 '0'
'Jones' 'F' 40 67 133 '0'
'Brown' 'F' 49 64 119 '0'
As for reading in multiple files in a directory, there is a easy method using datastore datastore allows you to specify file names with wild card.
  2 Comments
Neal
Neal on 7 Sep 2016
Mr.Otani I am attaching a sample data. Hope this makes my question much clearer.
Takuya Otani
Takuya Otani on 12 Sep 2016
Hi Neal,
I have created a little script as attached here. Please take a look. There are probably more elegant way to do this. I am pursing the file line-by-line removing unnecessary elements, and writing to a new file. Anyways, I have verified this works on your sample 001_NewYork.txt file, try this at your other file . Thanks Takuya

Sign in to comment.


per isakson
per isakson on 12 Sep 2016
Edited: per isakson on 14 Sep 2016
Given
  • only the numerical data of the files shall be imported
  • the values of the first column are ascending
  • the first 20 lines of the data files are headers.
  • the names of the data files starts with zero, two more digits and underscore.
  • the extension of the data files is '.txt'
I have downloaded your file 001_NewYork.txt to h:\m\cssm and made a copy, which I renamed to 002_Dallas.txt
With Matlab there are too many ways, by which these files can be imported and plotted. The script below show one way. (IMO: readtable is a bit of overkill for the task that you outline in your question.)
ffs = 'h:\m\cssm';
sad = dir( fullfile( ffs, '0*.txt' ) );
cac = regexp( {sad.name}, '^\d{3}_.+$', 'match' );
ise = cellfun( @isempty, cac );
cac = cac( not(ise) );
dat = cell( 1, length(cac) );
for jj = 1 : length( cac )
fid = fopen( fullfile( ffs, cac{jj}{1} ) );
dat(jj) = textscan( fid, '%f%f', 'Headerlines',20 ...
, 'CollectOutput',true );
fclose( fid );
end
figure
plot( dat{1}(:,1), dat{1}(:,2) )
figure
plot( dat{2}(:,1), dat{2}(:,2) )
Note that the text #####Extended Information will gracefully stop textscan. Doc says: "... and stops when it cannot match formatSpec to the data."
It might take some reading in the documentation to fully understand this script.

D. Ali
D. Ali on 27 Apr 2019
I have similar question where I need to extarct all MCAP with time they occured on in separat file and plot if possilbe
I attached the file
  4 Comments
per isakson
per isakson on 28 Apr 2019
Edited: per isakson on 28 Apr 2019
Yes, you did. (I didn't see them until you mentioned them.)
The first one, a couple of days ago, included very little information. The acronyms doesn't help much. That's the type of questions the are ignored or receives a link to the TUTORIAL in a comment.
The second one, ten hours ago, did get an answer. The responder posed a follow up question in a commnent: "let me know how you need to group these entries". You have not yet responded to that.
D. Ali
D. Ali on 28 Apr 2019
Yes I just saw the answe I will follow up thanks

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!