How can I improve the readability of a text file produced with writetable?
9 次查看(过去 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?
0 个评论
采纳的回答
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Database Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!