フィルターのクリア

How is the best choice to import many tables in matlab?

4 ビュー (過去 30 日間)
Giuseppe
Giuseppe 2022 年 3 月 19 日
コメント済み: Image Analyst 2022 年 3 月 19 日
Hi guys!
I'm working with tables importing in matlab. I've a database of 55 tables that store asteroids data and each table has: first line with asteroid name, header line and then data.
I would like to create a structure in matlab to store these tables. So, for example, the first field of structure contains the name of objects, the second field the position data, so x,y and z coordinates.
Something like that:
I managed to import position data, but I've problems to import names, that are positioned at first line and first column of my tables, as follows:
2012_LA
x_km, y_km, z_km
0.325319882863936E+08, -0.149108978103170E+09, 0.627986890338602E+07
0.351222909100528E+08, -0.148486401801382E+09, 0.637551116147345E+07
0.377011274331304E+08, -0.147819660954309E+09, 0.646921156241256E+07
0.402677620652748E+08, -0.147108971725492E+09, 0.656094421089712E+07
0.428214631350452E+08, -0.146354561948634E+09, 0.665068377342624E+07
0.453615032541851E+08, -0.145556671046544E+09, 0.673840548302432E+07
Here is my code with only 2 .csv input file for simplicity:
clc; clear all; close all;
% Import asteroid propagated orbits computed by Fortran program: "NEOs_orbits"
input_path = 'D:\OneDrive\MSc_Thesis\Projects\NEOs_orbits\OutputFiles\';
%Automatic .txt file detection in the aboved specified folders
files_ast_orbits = dir([input_path, '\*.csv']);
% Failed attempt to import asteroid name
%ast_database.pdes = readtable(fullfile(input_path,files_ast_orbits(36).name),"Range",'A1:A1')
for i = 1:numel(files_ast_orbits)
ast_database(i).pos = table2struct(readtable(fullfile(input_path,files_ast_orbits(i).name)));
end
Can you help me to also import asteroid names?
Can you give me your opinion about this structure? Is there a better way to create database made of many tables?

採用された回答

Voss
Voss 2022 年 3 月 19 日
% input_path = 'D:\OneDrive\MSc_Thesis\Projects\NEOs_orbits\OutputFiles\';
input_path = '.';
files_ast_orbits = dir(fullfile(input_path,'*.csv'));
% initialize a struct array with name and pos fields
ast_database = repmat(struct('name','','pos',[]),1,numel(files_ast_orbits));
for i = 1:numel(files_ast_orbits)
% read each file into a cell array
C = readcell(fullfile(input_path,files_ast_orbits(i).name));
% the name is the contents of the first cell
ast_database(i).name = C{1};
% convert the name to character vector if it is not already
if ~ischar(ast_database(i).name)
ast_database(i).name = num2str(ast_database(i).name);
end
% make the position data a matrix, store in the 'pos' field
ast_database(i).pos = reshape([C{3:end,:}],[],3);
end
% check it:
ast_database
ast_database = 1×2 struct array with fields:
name pos
{ast_database.name}
ans = 1×2 cell array
{'459872'} {'1991_VG'}
ast_database(1).pos
ans = 361×3
1.0e+08 * -0.1377 1.4274 0.1093 -0.1628 1.4261 0.1085 -0.1879 1.4245 0.1076 -0.2130 1.4224 0.1068 -0.2380 1.4199 0.1059 -0.2631 1.4169 0.1049 -0.2881 1.4136 0.1040 -0.3131 1.4098 0.1030 -0.3381 1.4056 0.1019 -0.3630 1.4010 0.1009

その他の回答 (1 件)

Image Analyst
Image Analyst 2022 年 3 月 19 日
Try importdata(). It gives you the header information separate from the numerical data.
  2 件のコメント
Giuseppe
Giuseppe 2022 年 3 月 19 日
Ok. It seems to works, but I have problem with first file because in this case the asteroid name is a number.
Can you help me?
Image Analyst
Image Analyst 2022 年 3 月 19 日
Looks like no name's code works because you accepted his answer.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeDatabase Toolbox についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by