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

14 ビュー (過去 30 日間)
Giuseppe
Giuseppe 2022 年 1 月 31 日
コメント済み: Enrico Gambini 2022 年 1 月 31 日
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?
Enrico Gambini
Enrico Gambini 2022 年 1 月 31 日
I think that you can set "FillValue","empty" in setvaropts

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeText Files についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by