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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/878915/image.png)
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"):
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/878920/image.png)
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!