Reading in Numerical Data from a Text File
45 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (2)
dpb
le 14 Jan 2026 à 21:22
Modifié(e) : dpb
le 14 Jan 2026 à 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 commentaires
Star Strider
le 14 Jan 2026 à 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 commentaires
Voir également
Catégories
En savoir plus sur Data Import and Export dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



