Separation of Specific Columns from a Text File

1 回表示 (過去 30 日間)
Sohel Rana
Sohel Rana 2021 年 12 月 23 日
回答済み: Voss 2021 年 12 月 23 日
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 件のコメント
dpb
dpb 2021 年 12 月 23 日
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

サインインしてコメントする。

回答 (1 件)

Voss
Voss 2021 年 12 月 23 日
"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);
data = 11×4
1.0e+03 * 1.4600 -0.0528 -0.0530 -0.0440 1.4600 -0.0514 -0.0522 -0.0433 1.4600 -0.0503 -0.0527 -0.0431 1.4600 -0.0497 -0.0524 -0.0437 1.4600 -0.0514 -0.0531 -0.0441 1.4600 -0.0507 -0.0525 -0.0433 1.4600 -0.0497 -0.0513 -0.0432 1.4601 -0.0483 -0.0523 -0.0433 1.4601 -0.0504 -0.0525 -0.0434 1.4601 -0.0488 -0.0529 -0.0429
% lam = smoothdata(data(:,1),'gaussian');

カテゴリ

Help Center および File ExchangeLarge Files and Big Data についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by