How to import variable names and specific data range (comma separated values) from a .txt file?
조회 수: 7 (최근 30일)
이전 댓글 표시
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)
댓글 수: 0
채택된 답변
Stephen23
2022년 3월 15일
편집: Stephen23
2022년 3월 15일
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)
댓글 수: 8
Stephen23
2022년 3월 15일
편집: Stephen23
2022년 3월 15일
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.
추가 답변 (1개)
Arif Hoq
2022년 3월 15일
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]
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!