Access data from table element
이전 댓글 표시
I would like to extrapolate a specific number from a table element, namely the thir one in thi case. The routine that reads .txt files is:
for n=1:numfiles
c{n} = readtable(s(n).name);
a = file_list(n);
RMS_spot_radius=c{1};
T=RMS_spot_radius;
T_=T(5,1)
end
For each file it returns:
T= Title
__________________________________________________________________________________________________________________________
{'0.55000000 - 0.55000000 →0.00006669 →0.00013316 →1.08604530 →0.76784704 →0.76805294'}
How do I access only the third number here?
Thank you!
댓글 수: 2
Johannes Hougaard
2021년 12월 9일
If you attach the .txt file (or maybe even two of them) it's a lot easier to help you.
Giulia Panusa
2021년 12월 9일
답변 (1개)
Mathieu NOE
2021년 12월 9일
편집: Mathieu NOE
2021년 12월 9일
hello Giulia
try this
clc
clearvars
s = dir('00*.txt')
numfiles = numel(s);
for n=1:numfiles
filename = s(n).name;
T = readtable(s(n).name,'headerlines',9);
% a = file_list(n); % could not run this code
RMS_spot_radius(n,1) = T.Radius;
end
댓글 수: 3
Giulia Panusa
2021년 12월 10일
Johannes Hougaard
2021년 12월 10일
No, the 'headerlines' is an input that describes how many lines in your text file that is containing information rather than data. Changing the 'headerlines' would be needed if your text file is not formatted the same way with your column headers (Wavelength, Centroid-X, Centroid-Y etc) on the 10th line and you actual data on the 11th line and below.
Setting the 'headerlines' correct leads to a table (T) where the variables are named according to their header..if you change the 'headerline' to 10 it will create a very unuseful table with no variable names and loads of unuseful variables.
I do, however, see an issue with your data as the 'Wavelength' apparently is not read correct with the above code.
I would therefore add an additional option to the T = readtable to force the function to only separate at 'tab' not at 'space'
file_list = dir('0*.txt');
RMS_spot_radius = nan(size(file_list));
for ii = 1:length(file_list)
T = readtable(file_list(ii).name,'NumHeaderLines',9,'delimiter','\t');
RMS_spot_radius(ii) = T.RMSSpotRadius;
end
this will hopefully get you a table with the correct named variables
Giulia Panusa
2021년 12월 10일
카테고리
도움말 센터 및 File Exchange에서 Data Type Identification에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!