Separation of Specific Columns from a Text File
2 views (last 30 days)
Show older comments
I don't know what is wrong in my code to select specfic column from the following data. In my data there are four columns that you can see. The text file also conatins string as well. I used the following code to selcet first column, which starts with 1460. I would appreciate your help. Thanks.
sohel=fopen('FB.txt','r');
data=textscan(sohel,'%f%f','delimiter', '\t');
rubel=fclose(sohel);
lam=smoothdata(data{1},'gaussian');
(I presume following is replicate of the file -- used code button to format so folks could copy for test... --dpb)
HW Acquisition Rate: 1000 Hz
Wavelength Tracking: 400 pm / acquistion
Data Interleave: 1
CH 1 Configuration:
Distance Compensation Enabled: False
Spectral Average Count: 1
Detect Valley: False
Detection Setting ID: 128
CH 2 Configuration:
Distance Compensation Enabled: False
Spectral Average Count: 1
Detect Valley: False
Detection Setting ID: 128
CH 3 Configuration:
Distance Compensation Enabled: False
Spectral Average Count: 1
Detect Valley: False
Detection Setting ID: 128
CH 4 Configuration:
Distance Compensation Enabled: False
Spectral Average Count: 1
Detect Valley: False
Detection Setting ID: 128
Wvl (nm) CH 1 CH 3 CH 4
1460.0000 -52.7734 -52.9736 -43.9522
1460.0080 -51.3987 -52.2463 -43.3339
1460.0160 -50.3044 -52.6952 -43.1207
1460.0240 -49.6716 -52.4275 -43.6564
1460.0320 -51.4409 -53.0774 -44.0559
1460.0400 -50.6857 -52.5140 -43.3023
1460.0480 -49.7279 -51.3204 -43.1532
1460.0560 -48.2821 -52.3089 -43.3152
1460.0640 -50.3557 -52.4843 -43.3857
1460.0720 -48.7783 -52.9036 -42.9076
1460.0800 -51.3987 -52.6301 -42.8598
1 Comment
dpb
on 23 Dec 2021
data=textscan(sohel,'%f%f','delimiter', '\t');
will fail on the header lines...use the headerlines parameter to skip if known fixed number or scan the file looking for the column variable names lines if can vary.
Optionally, look into detectimportoptions and use one of the higher-level routines like readtable or readmatrix
Answers (1)
Voss
on 23 Dec 2021
"The text file also conatins string as well."
This is the problem. You would need to tell textscan() to skip that header text. Here is one way to do that:
(I'm attaching a file I made from copying and pasting the text from your question into a text editor. It may not be the same as your actual text file. If you have a problem running this code, upload your actual text file.)
% open the file
fid = fopen('FB.txt','r');
% read lines of text until you get to a line that starts with 'Wvl (nm)'
str = '';
while ~startsWith(str,'Wvl (nm)')
str = fgetl(fid);
end
% now get the data
data = textscan(fid,'%f');
% close the file
fclose(fid);
% reshape the data into an n-by-4 matrix
data = reshape(data{1},4,[]).';
display(data);
% lam = smoothdata(data(:,1),'gaussian');
0 Comments
See Also
Categories
Find more on Text Data Preparation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!