Reading a variable that have different names in different data files
6 ビュー (過去 30 日間)
古いコメントを表示
assuming I am using the following lines to read some data files that have a table structure
for k = 1 : nfiles
fullFileName = fullnames{k};
%reading the file as a table
opts = detectImportOptions(fullFileName);
getvaropts(opts,{'YEAR','MONTH','DAY','HOUR','MIN','GDALT','NE8'});
opts.SelectedVariableNames = {'YEAR','MONTH','DAY','HOUR','MIN','GDALT','NE8'};
t = readtable(fullFileName,opts);
end
later on I have discovred that some files do not have the variable NE8 or GDALT, or they have them under different names, how can I exclude the files that does not have these variables and not read them and how can I read them from different files under differnet names ?
2 件のコメント
Walter Roberson
2021 年 10 月 2 日
detectImportOptions and then examine the variable names returned in the options structure, to see whether it has the variables you need
getvaropts(opts,{'YEAR','MONTH','DAY','HOUR','MIN','GDALT','NE8'});
That line is not doing anything useful for you -- not unless you remove the semi-colon so that you can display the output.
採用された回答
Walter Roberson
2021 年 10 月 2 日
The assumption below is that if GDALT is missing then you do not want the file, but that if NE8 is missing then you can use POP interchangably, and that if both NE8 and POP are missing then you do not want the file.
needed_vars = {'YEAR','MONTH','DAY','HOUR','MIN','GDALT'};
wanted_one_of_vars = {'NE8', 'POP'};
opts = detectImportOptions(fullFileName);
vars = opts.VariableNames;
if all(ismember(needed_vars, vars)) && any(ismember(wanted_one_of_vars, vars))
if ismember(wanted_one_of_vars{1}, opts)
NE8_varname = wanted_one_of_vars{1};
else
NE8_varname = wanted_one_of_vars{2};
end
else
continue; %or whatever you need to do for files that do not have all the variables
end
opts.SelectedVariableNames = [needed_vars, {NE8_varname}];
t = readtable(fullFileName,opts);
3 件のコメント
Walter Roberson
2021 年 10 月 10 日
needed_vars = {'YEAR','MONTH','DAY','HOUR','MIN','GDALT'};
wanted_one_of_vars = {'NE8', 'POP'};
opts = detectImportOptions(fullFileName);
vars = opts.VariableNames;
if all(ismember(needed_vars, vars)) && any(ismember(wanted_one_of_vars, vars))
if ismember(wanted_one_of_vars{1}, vars)
NE8_varname = wanted_one_of_vars{1};
else
NE8_varname = wanted_one_of_vars{2};
end
else
continue; %or whatever you need to do for files that do not have all the variables
end
opts.SelectedVariableNames = [needed_vars, {NE8_varname}];
t = readtable(fullFileName,opts);
その他の回答 (1 件)
Sulaymon Eshkabilov
2021 年 10 月 2 日
Have you tried this ways of reading data, e.g.:
for k = 1 : nfiles
fullFileName = fullnames{k};
%reading the file as a table
opts = detectImportOptions(fullFileName);
t{k} = readtable(fullFileName,opts);
end
That results in k number of tables residing in a cell array variable called t that can be separated via another step.
3 件のコメント
Sulaymon Eshkabilov
2021 年 10 月 2 日
Yes, indeed what you are saying is correct. Maybe in this case, you had better import just part of the data by indexes and ignore the rest without using the variable names since they are not consistent for all data files.
参考
カテゴリ
Help Center および File Exchange で Tables についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!