Clear Filters
Clear Filters

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 views (last 30 days)
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

Accepted Answer

Voss
Voss on 8 Oct 2022
Edited: Voss on 8 Oct 2022
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.)

More Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!