opts = detectImportOptions Pass method when there is no variable Naming when reading more than 100 files

4 views (last 30 days)
Background: Using opts = detectImportOptions, some file data are missing out of more than 100 files
Purpose: How to write after skipping the 25th file VariableName2 and skipping
path = pwd;
list = dir(path);
S = dir(fullfile(path, '*.csv'));
for k = 1:numel(S)
F = fullfile(S(k).folder,S(k).name);
opts = detectImportOptions(fullfile(S(k).folder,S(k).name),"VariableNamingRule","preserve",ImportErrorRule="omitrow",MissingRule="omitrow");
% There is no VariableName2 due to the omission of some file data among more than 100 files,
opts.SelectedVariableNames = {'VariableName1', 'VariableName2'};
% Error occurs because there is no VariableName2
opts.MissingRule = 'omitrow';
T = readtable(F,opts,"ReadVariableNames",true);
filename = getfield(S,{k},'name');
writematrix(filename,'naming.csv','Delimiter',',','QuoteStrings','all','WriteMode','append');
writematrix(matrix_data,'myData.csv','Delimiter',',','QuoteStrings','all','WriteMode','append');
end

Answers (1)

Aditya
Aditya on 26 Jun 2024
Hi 성 황,
To handle the issue of missing variables (e.g., VariableName2) when reading multiple files with 'detectImportOptions', you can add a check to ensure the variable exists before setting the 'SelectedVariableNames'. Additionally, you can skip the 25th file as requested.
Here is an updated version of your code:
path = pwd;
list = dir(path);
S = dir(fullfile(path, '*.csv'));
for k = 1:numel(S)
% Skip the 25th file
if k == 25
continue;
end
F = fullfile(S(k).folder, S(k).name);
opts = detectImportOptions(F, "VariableNamingRule", "preserve", ...
"ImportErrorRule", "omitrow", "MissingRule", "omitrow");
% Check if 'VariableName2' exists in the file
if ismember('VariableName2', opts.VariableNames)
opts.SelectedVariableNames = {'VariableName1', 'VariableName2'};
else
opts.SelectedVariableNames = {'VariableName1'};
end
% Read the table
T = readtable(F, opts, "ReadVariableNames", true);
% Write the filename to 'naming.csv'
filename = S(k).name;
writematrix(filename, 'naming.csv', 'Delimiter', ',', 'QuoteStrings', 'all', 'WriteMode', 'append');
% Write the table data to 'myData.csv'
writematrix(T, 'myData.csv', 'Delimiter', ',', 'QuoteStrings', 'all', 'WriteMode', 'append');
end
I hope this helps!

Categories

Find more on Timetables in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!