Reading a variable that have different names in different data files

조회 수: 5(최근 30일)
MA
MA 2021년 10월 2일
댓글: MA 2021년 10월 12일
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
MA
MA 2021년 10월 2일
This is the error I am getting
Error using matlab.io.ImportOptions/getNumericSelection (line 518)
Unknown variable name: 'POP'.
Error in matlab.io.ImportOptions/set.SelectedVariableNames (line 178)
rhs = getNumericSelection(obj,rhs);
Error in extractingtables (line 30)
opts.SelectedVariableNames = {'YEAR','MONTH','DAY','HOUR','MIN','GDALT','NE8','POP'};
where in one of the files we have such variable as seen in the inage below:
There are files that contain the NE8 variables , files that contain it under the name POP and files that does not have it at all. I am getting errors for both the second case and third case since not all the files have 'POP' and sone files does not have 'NE8'

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

채택된 답변

Walter Roberson
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

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

추가 답변(1개)

Sulaymon Eshkabilov
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
MA
MA 2021년 10월 2일
yes I thought of that as well but the issue is the columns of the variables 'GDALT'and 'NE8' are having different places in different files, at one file they may be the 6th and 8th column at another they may be the 10th and 15th, that is why I am using there names in the first place, but now NE8 appear in a deffirent name as well in some files.

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

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by