필터 지우기
필터 지우기

When checking to see if a variable has been created within a struct, "exist" and "isfield" both come back 0 when the struct variable exists and is populated.

조회 수: 2 (최근 30일)
I'm using an EEGLab function to clean some data. If it determines a row has a significant amount of noise and should be removed, it creates a new variable in the stuct. If no rows are removed, this variable is not created.
The code is set up to check for which, if any rows have been removed by the cleaning function.
I've tried using both the exist() and isfield() functions, both return logical 0, but when I open the struct from the Workspace, it clearly shows that variable in the struct and populated with data.
I've tried both isfield('EEG_temp','etc.clean_channel_mask') and isafield('EEG_temp.etc','clean_channel_mask') but receive the same results. (code and results below, also a screen grab showing that EEG_temp struct does include the populated variable.)
If done manually, bypassing the "if" statement and using exists or isfield directly, even when they boht return logical 0, the ch_bad loads the correct data.
Any suggestions on how to get this working correctly? Thank you.
EEG_test = clean_rawdata(EEG, flatline, hpf_data, corr_threshold, linenoise, repair_busrsts_std, remove_timewindows);
if exist('EEG_temp.etc.clean_channel_mask','var') % Load the clean_channel_mask from the EEG struct
ch_bad = EEG_test.etc.clean_channel_mask; % copy that mask to ch_bad (0 = bad channel)
Both exist and isafield return
isfield('EEG_temp','etc.clean_channel_mask')
ans =
logical
0

채택된 답변

Voss
Voss 2022년 10월 8일
편집: Voss 2022년 10월 8일
Do it like this:
isfield(EEG_temp,'etc') && isfield(EEG_temp.etc,'clean_channel_mask')
In other words, check that 'etc' is a field of EEG_temp first, then check that 'clean_channel_mask' is a field of EEG_temp.etc.
(Note that you should not have quotes around the first argument to isfield, as in isfield('EEG_temp','etc'), because 'EEG_temp' is a character vector, which has no fields (since it's not a struct), so of course isfield returns false.)

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by