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!