How to import variable names and specific data range (comma separated values) from a .txt file?

Hi guys,
I read lots of similar questions but I cannnot solve the problem. I've downloaded Earth ephemerides from NASA JPL website(attached to this question there is the .txt file) and I need to extract only variable names line (row 23) and data from row 26 up to 29.
I want to avoid Matlab tool "import data", but rather I woulde prefer to use something like ""readtable" or similar. I tried to use readtable but I've troubles.
I tried to code something like that but it seems to not take the right lines.
clc; clear all; close all
% Ephemerides
file_name = 'Earth-Moon_sys_ephem_at_ast_epochs.txt';
opts = detectImportOptions(file_name)
opts.DataLines = [26, 29];
opts.VariableNamesLine = 34;
opts.VariableNames
opts.VariableTypes = ["double", "string", "double", "double", "double", "double", "double",...
"double", "double", "double", "double","double","double","double"];
EM_sys_eph = readtable(file_name ,opts)

 Accepted Answer

Make sure you specify the delimiter and (start) Range for DETECTIMPORTOPTIONS:
file_name = 'Earth-Moon_sys_ephem_at_ast_epochs.txt';
opts = detectImportOptions(file_name, 'Range',26, 'Delimiter',',');
opts.DataLines = [26,29];
opts.VariableNamesLine = 23;
opts.VariableTypes = ["double", "string", repmat("double",1,12)];
opts.VariableNamingRule = 'preserve';
EM_sys_eph = readtable(file_name, opts)
EM_sys_eph = 4×14 table
JDTDB Calendar Date (TDB) EC QR IN OM W Tp N MA TA A AD PR __________ ________________________________ ________ __________ _________ ______ ______ __________ __________ ______ ______ _________ __________ __________ 2.4545e+06 "A.D. 2008-Mar-06 00:00:00.0000" 0.016737 1.4709e+08 0.0010567 173.6 289.27 2.4545e+06 1.1408e-05 61.112 62.808 1.496e+08 1.521e+08 3.1558e+07 2.4576e+06 "A.D. 2016-Jul-09 00:00:00.0000" 0.016735 1.4709e+08 0.0021419 173.05 289.83 2.4578e+06 1.1408e-05 184.25 184.11 1.496e+08 1.521e+08 3.1557e+07 2.4589e+06 "A.D. 2020-Mar-17 00:00:00.0000" 0.01675 1.4709e+08 0.002633 176.77 286.22 2.4589e+06 1.1407e-05 71.765 73.6 1.496e+08 1.521e+08 3.1558e+07 2.4596e+06 "A.D. 2022-Jan-21 00:00:00.0000" 0.01669 1.471e+08 0.0028082 174.65 288.52 2.4596e+06 1.1408e-05 16.87 17.437 1.496e+08 1.5209e+08 3.1558e+07

8 Comments

Many thanks! Can you tell me the differences and the possible issues with my recent code attempt:
% Ephemerides
file_name = 'Earth-Moon_sys_ephem_at_ast_epochs.txt';
EM_sys_ephem = readtable(file_name,"Range",'A23:N29',"Delimiter",'comma','VariableNamingRule',"preserve")
My output:
Stephen,can you please explain the cause of this error whenever I execute your code ?
Unknown Parameter 'VariableNamingRule'.
"can you please explain the cause of this error whenever I execute your code ?"
Possibly because you are not using the most recent version (as this forum does).
I moved that option to the OPTS object, it might be more robust there.
@Stephen can you give me your opinion about my first comment in this answer, please?
Thank you in advance!
"Can you tell me the differences and the possible issues with my recent code attempt:"
Two issues are clear when we simply look at the data:
  • your code imports superfluous header lines as data
  • those superfluous lines force READTABLE to import the first column as string.
file_name = 'Earth-Moon_sys_ephem_at_ast_epochs.txt';
EM_sys_ephem = readtable(file_name,"Range",'A23:N29',"Delimiter",'comma')
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
EM_sys_ephem = 6×14 table
JDTDB CalendarDate_TDB_ EC QR IN OM W Tp N MA TA A AD PR ______________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________ __________________________________ ________ __________ _________ ______ ______ __________ __________ ______ ______ _________ __________ __________ {'**************************************************************************************************************************************************************************************************************************************************************************************************************************************************'} {0×0 char } NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN {'$$SOE' } {0×0 char } NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN {'2454531.500000000' } {'A.D. 2008-Mar-06 00:00:00.0000'} 0.016737 1.4709e+08 0.0010567 173.6 289.27 2.4545e+06 1.1408e-05 61.112 62.808 1.496e+08 1.521e+08 3.1558e+07 {'2457578.500000000' } {'A.D. 2016-Jul-09 00:00:00.0000'} 0.016735 1.4709e+08 0.0021419 173.05 289.83 2.4578e+06 1.1408e-05 184.25 184.11 1.496e+08 1.521e+08 3.1557e+07 {'2458925.500000000' } {'A.D. 2020-Mar-17 00:00:00.0000'} 0.01675 1.4709e+08 0.002633 176.77 286.22 2.4589e+06 1.1407e-05 71.765 73.6 1.496e+08 1.521e+08 3.1558e+07 {'2459600.500000000' } {'A.D. 2022-Jan-21 00:00:00.0000'} 0.01669 1.471e+08 0.0028082 174.65 288.52 2.4596e+06 1.1408e-05 16.87 17.437 1.496e+08 1.5209e+08 3.1558e+07
@Stephen sorry, I missed something on my first attempt code. On my Matlab I get the output I attached in my first edited comment.
If it works on your version, then:
  • go for it, use it
  • keep in mind that it might not work on other versions or OSs
The function READTABLE et al are being developed continuously, and can give different outputs for different versions (as we can see on this forum). Only you can decide if that is important for your work or not.

Sign in to comment.

More Answers (1)

try this:
A=readtable('Earth-Moon_sys_ephem_at_ast_epochs.txt','delimiter','tab');
B=table2cell(A(20,:));
parameter=split(B,',')';
C=table2cell(A(23:26,:));
data=split(C,',');
output=[parameter;data]

Categories

Products

Release

R2021a

Asked:

on 15 Mar 2022

Edited:

on 15 Mar 2022

Community Treasure Hunt

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

Start Hunting!