How can I create a rule to automatically fill blank cells during the table reading with readtable function?
Ältere Kommentare anzeigen
Hi guys!
I read a .csv file in matlab following this procedure: home -> importdata -> select my .csv file. Then I properly set the data type of first two columns by selecting text(string). Some cells of the second column are empty and when I recall the variable in the command window I get "".
Then I filter my data and once exported them in a .txt file I have empty cells: this can be a problem when I will read the data in my Fortran program. So, I would like to fill the blank space with a default string, such as "(Empty cell)" or something like that.
Can you help me?
Here the code I produced by following the above procedure:
clear all; close all; clc;
%% Data import from .CSV files
file_name_asteroids = 'NEOs_asteroids.csv';
%Asteroid data reading
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 = 5; % (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 for Fortran calculations
Output_file_name = 'NEOs_asteroids_filtered.txt';
writetable(Ast_data_filtered,Output_file_name,...
"WriteVariableNames",true,"Encoding",'UTF-8',"Delimiter","tab");
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu String Parsing finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
