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

조회 수: 7 (최근 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?
Enrico Gambini
Enrico Gambini 2022년 1월 31일
I think that you can set "FillValue","empty" in setvaropts

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Database Toolbox에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by