Reading a variable that have different names in different data files
Show older comments
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 Comments
Walter Roberson
on 2 Oct 2021
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.
Accepted Answer
More Answers (1)
Sulaymon Eshkabilov
on 2 Oct 2021
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 Comments
MA
on 2 Oct 2021
Sulaymon Eshkabilov
on 2 Oct 2021
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.
MA
on 2 Oct 2021
Categories
Find more on Text Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!