Reading in Numerical Data from a Text File
9 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (2)
dpb
el 14 de En. de 2026 a las 21:22
Editada: dpb
el 14 de En. de 2026 a las 21:30
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 comentarios
Star Strider
el 14 de En. de 2026 a las 22:09
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 comentarios
Ver también
Categorías
Más información sobre Data Import and Export en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



