Extracting data from text file.
5 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposte (3)
Takuya Otani
il 7 Set 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 Commenti
Takuya Otani
il 12 Set 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
per isakson
il 12 Set 2016
Modificato: per isakson
il 14 Set 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.
0 Commenti
D. Ali
il 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 Commenti
per isakson
il 28 Apr 2019
Modificato: per isakson
il 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.
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!