How is the best choice to import many tables in matlab?
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Giuseppe
el 19 de Mzo. de 2022
Comentada: Image Analyst
el 19 de Mzo. de 2022
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?
0 comentarios
Respuesta aceptada
Voss
el 19 de Mzo. de 2022
% 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.name}
ast_database(1).pos
0 comentarios
Más respuestas (1)
Image Analyst
el 19 de Mzo. de 2022
Try importdata(). It gives you the header information separate from the numerical data.
2 comentarios
Image Analyst
el 19 de Mzo. de 2022
Looks like no name's code works because you accepted his answer.
Ver también
Categorías
Más información sobre Database Toolbox en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!