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 Comments

If you attach the .txt file (or maybe even two of them) it's a lot easier to help you.
Dear Johannes,
here you are :) thank you!

Sign in to comment.

Answers (1)

Mathieu NOE
Mathieu NOE on 9 Dec 2021
Edited: Mathieu NOE on 9 Dec 2021
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 Comments

Dear Mathieu,
thank you so much! It works, even if I am noticing that I have to pay attention to the right parameter to take.
So basically, by typing : 'headerlines',9
I am creating a new table where I am setting new headerlines which corresponds to each word of the line that is separatated with a space tab, is that right? If I change the number '9' I am changing the size of the new table I am crating, but I am not exactly understanding how. Could you clarify this point to me please?
In this specific example, the parameter I am interested in is the RMS spot radius (T.Radius), but in case I would need to take the centroid x, I shoud type T.RMS, and in this I should be careful !
Thanks again,
take care,
Giulia.
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
Dear Johannes,
thank you as well, it works.
Thank you guys,
take care,
Giulia.

Sign in to comment.

Categories

Products

Release

R2021b

Asked:

on 9 Dec 2021

Commented:

on 10 Dec 2021

Community Treasure Hunt

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

Start Hunting!