How can I improve the readability of a text file produced with writetable?

10 次查看(过去 30 天)
Hi guys!
I want to export a matlab table as .txt file by using the function writetable but I've the problem that the exported data are "disordered" in the new file.
My output is:
I would like to achieve something very similar to matlab table, i.e. aligned columns (note that the column of "name" is empty, so I would like to fill each cell with a string "empty"):
Here my piece code:
clear all; close all; clc;
file_name_asteroids = 'NEOs_asteroids.csv';
opts = delimitedTextImportOptions("NumVariables", 11);
% Specify range and delimiter
opts.DataLines = [2, Inf];
opts.Delimiter = ",";
% Specify column names and types
opts.VariableNames = ["pdes", "name", "epoch", "a", "e", "i", "om", "w", "ma", "q", "ad"];
opts.VariableTypes = ["string", "string", "double", "double", "double", "double", "double", "double", "double", "double", "double"];
% Specify file level properties
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
% Specify variable properties
opts = setvaropts(opts, ["pdes", "name"], "WhitespaceRule", "preserve");
opts = setvaropts(opts, ["pdes", "name"], "EmptyFieldRule", "auto");
% Import the data
Ast_data = readtable(file_name_asteroids,opts);
%data filtering
i_max = 10; % (deg)
e_max = 0.1;
q_min = 0.9; %(AU)
ad_max = 1.1; % (AU)
Ast_cond = Ast_data.i <= i_max & Ast_data.e <= e_max &...
Ast_data.q >= q_min & Ast_data.ad <= ad_max;
Ast_data_filtered = Ast_data(Ast_cond,:);
%data export
Output_file_name = 'NEOs_asteroids_filtered.txt';
writetable(Ast_data_filtered,Output_file_name,...
"WriteVariableNames",true,"Encoding",'UTF-8',"Delimiter","tab");
Can you help me to obtain the desired table and to fill the empty column?

采纳的回答

Enrico Gambini
Enrico Gambini 2022-1-31
Hi, I think that the export worked well. If you want to increase the readability of the textfile i think you should set less decimals in your numeric values (i.e., reduce precision): the column names will be more "centered" with respect to values.
To fill the column you can do the following.
Ast_data_filtered.name(1:end)={"empty"};
  2 个评论
Giuseppe
Giuseppe 2022-1-31
Hi @Enrico Gambini, thanks for your answer. I need all the decimal digits. Isn't there a way to automatically detect and fill the empty cells during the import of table with readable function?

请先登录,再进行评论。

更多回答(0 个)

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by