Reading in Numerical Data from a Text File
35 views (last 30 days)
Show older comments
I want to read in the numerical data from the data file to plot, while excluding the first 4 lines of text. I've tried using textscan for this, but haven't got it working yet. Any help would be appreciated.
0 Comments
Answers (2)
dpb
about 3 hours ago
Edited: dpb
about 3 hours ago
d=dir('*.txt'); % get long name easily
l=readlines(d.name); % read as text so
[l(1:10); l(end-9:end)] % can see what file contains
tData=readtable(d.name,'numheaderlines',3,'readvariablenames',1); % it's got variable names after three header lines
head(tData) % show beginning of what we got
plot(tData.X,tData.Y) % plot it...
Alternatively, if don't want to use the table,
XY=readmatrix(d.name,'numheaderlines',4); % 2D array, no variable names
plot(XY(:,1),XY(:,2)) % plot the columns of the array
There appears to be one (or maybe a couple) really big outlier.
ixBig=find(tData.Y>1E6)
tData.Y(ixBig)=nan; % just ignore it for now...could interpolate
plot(tData.X,tData.Y) % plot again w/o the bum point
0 Comments
Star Strider
about 2 hours ago
You can certainly use textscan for this.
I cannot determine the reason you are having probnlems getting textscan to run, since you did not post your code.
Try something like this --
filename = '4-21 »» Derive...S_ACN_2uM.txt';
type(filename) % File Content Information
fidi = fopen(filename);
data = textscan(fidi, '%f%f', 'HeaderLines',4, 'CollectOutput',1)
fclose(fidi);
figure
plot(data{:}(:,1), data{:}(:,2))
grid
xlabel('X')
ylabel('Y')
xy = cell2mat(data)
figure
plot(xy(:,1), xy(:,2))
grid
xlabel('X')
ylabel('Y')
.
0 Comments
See Also
Categories
Find more on Data Import and Export in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


