How to skip an empty field of opts.selec​tedvariabl​enames...

조회 수: 3 (최근 30일)
Tyann Hardyn
Tyann Hardyn 2021년 8월 25일
답변: Tyann Hardyn 2021년 8월 26일
Hi everyone. Im sorry if this question is rather weird or something. But i just want to create a condition that allow my blank data field from a file which i read from readtable function become readable (as a blank data or the other replacing rule). So i have this code :
[namafile,direct1]=uigetfile({'*.txt', 'Text-files (*.txt)'},'Load Data Magnet Format IAGA (Definitif / Variatif)', 'Multiselect','on');
cd(direct1);
full = fullfile(direct1, namafile);
[~, base, ~] = fileparts(full);
judul = length(base);
defm = sum(contains(base, 'dmin'));
varm = sum(contains(base, 'vmin'));
hord = sum(contains(base, 'dhor'));
if judul == 11 || hord == 1
stasiun1 = upper(base(1:3));
std = upper(extractBetween(base,1,3))+"D"; %This data are get from the title of file (base) but it sometimes didnt
%appear inside the data
%of each file
sth = upper(extractBetween(base,1,3))+"H";
stz = upper(extractBetween(base,1,3))+"Z";
stf = upper(extractBetween(base,1,3))+"F";
stx = upper(extractBetween(base,1,3))+"X";
sty = upper(extractBetween(base,1,3))+"Y";
stt = lower(base(end-3:end));
opts = detectImportOptions(full,"NumHeaderLines",12, 'EmptyFieldRule', 'missing');
opts.MissingRule = 'omitvar';
opts.SelectedVariableNames = ["DATE","TIME",std,sth,stz,stf,stx,sty];%I just want to filter the data
% so although the 'std' data was not
% appear or cannot be readed by
% opts.SelectedVariableNames, it would be
% skipped and did not make a message
% alert
T = readtable(full,opts);
DATE = T.DATE;
TIME = T.TIME;
TIME.Format = 'hh:mm:ss';
DOD = T.(std);
DOH = T.(sth);
DOZ = T.(stz);
DOF = T.(stf);
DOX = T.(stx);
DOY = T.(sty);......................
lambar = isempty(DOD);
switch lambar
case lambar == 0.........
When the code was executed, it will show an alert message like this :
Error using matlab.io.ImportOptions/getNumericSelection (line 498)
Unknown variable name: 'KPGD'.
Error in matlab.io.ImportOptions/set.SelectedVariableNames (line 166)
rhs = getNumericSelection(obj,rhs);
Error in teslemiiaga (line 218)
opts.SelectedVariableNames = ["DATE","TIME",std,sth,stz,stf,stx,sty];
Iam really know that the variable of std, which is 'KPGD' (KPG are get from the title of file), is missing there, but i just want to skip it or ignore it as a blank data so i can continue with the code of :
lambar = isempty(DOD);
switch lambar
case lambar == 0
I have to skip or ignore (and not to remove it instead) the variable of std from opts.SelectedVariableNames because the other data is sometimes may contain with that variable of std. (i have already attaching the data). And when i read the opts.SelectedVariableNames which contain with no std variables in it, it will show cells array like this :
1×8 cell array
{'DATE'} {'TIME'} {'DOY'} {'KPGX'} {'KPGY'} {'KPGZ'} {'KPGF'} {'x_'}% <--- This one is should be KPGD if the field was exist
Would anyone in this lovely matlab community lend me a hand to solve this problem, please? Iam so grateful if someone can give me the best solution or giving a logical problem solving here... Thank you very much /.\ /.\ /.\

채택된 답변

Tyann Hardyn
Tyann Hardyn 2021년 8월 26일
Oh, i have already solved this quetion of mine after so many doing try and error...
if judul == 11 || hord == 1
stasiun1 = upper(base(1:3));
std = upper(extractBetween(base,1,3))+"D";
stds = convertStringsToChars(std);
sth = upper(extractBetween(base,1,3))+"H";
stz = upper(extractBetween(base,1,3))+"Z";
stf = upper(extractBetween(base,1,3))+"F";
stx = upper(extractBetween(base,1,3))+"X";
sty = upper(extractBetween(base,1,3))+"Y";
tip1 = lower(base(end-3:end));
opts = detectImportOptions(full,"NumHeaderLines",12, 'EmptyFieldRule', 'missing', 'Whitespace', '');
switch lambar == sum(contains(opts.SelectedVariableNames, stds))
case lambar == 0
opts.SelectedVariableNames = ["DATE","TIME",stz,stx,sty,stf];
T = readtable(full,opts);
DATE = T.DATE;
TIME = T.TIME;
TIME.Format = 'hh:mm:ss';
DOZ = T.(stz);
DOX = T.(stx);
DOYs = T.(sty);
DOF = T.(stf);
periode = DATE + TIME;............ continue with the code
case lambar == 1
opts.SelectedVariableNames = ["DATE","TIME",std,sth,stz,stf];
% Treat 'NA' as missing values in all variables
T = readtable(full,opts);
DATE = T.DATE;
TIME = T.TIME;
TIME.Format = 'hh:mm:ss';
DOD = T.(std);
DODS = DOD./60; %Karena Nilai D (DOD) masih dalam Minutes Of Arc%
DOH = T.(sth);
DOZ = T.(stz);
DOF = T.(stf);............ continue with the code
Case closed....

추가 답변 (0개)

Community Treasure Hunt

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

Start Hunting!

Translated by