Why am I getting this error when trying to logically compare 2 arrays inside of a struct of the same length

2 views (last 30 days)
Assigning to 289120 elements using a simple assignment statement is not supported. Consider using
comma-separated list assignment.
Error in SARA_Altitude_processing (line 35)
data(MainRawFilt).(dataNames{k}) = [];
Related documentation
>> data(MainRawFilt).(dataNames{k})
The logical indices contain a true value outside of the array bounds.
>> length(MainRawFilt),length(data.(dataNames{k}))
ans =
806488
ans =
806488
>> size(MainRawFilt),size(data.(dataNames{k}))
ans =
806488 1
ans =
806488 1
This is the sample of the code that is causing error
dataNames = fieldnames(data);
for k = 1:length(dataNames)
dataNm = dataNames(k);
if length(data.(dataNames{k})) == length(data.mainRawAlt)
MainRawFilt = data.mainRawAlt == 8888;
data(MainRawFilt).(dataNames{k}) = [];
MainRawFilt = data.mainRawAlt == 6000;
data(MainRawFilt).(dataNames{k}) = [];
MainRawFilt = (data.mainRawAlt > 5783) & (data.mainRawAlt < 5784);
data(MainRawFilt).(dataNames{k}) = [];
end
end
the variable MainRawFilt is a 806488x1 logical
and i am trying to loop through all of the fields of the struct Data that are the same length as MainRawFilt to filter out certain values across all the fields of that length.

Answers (2)

Jeffrey Clark
Jeffrey Clark on 16 Jul 2022
@Bolaji, I believe that with each of the statements data(MainRawFilt).(dataNames{k}) = [] you are reducing the size of data.(dataNames{k}) without correspondingly reducing the size of data.mainRawAlt, so that the size of the next MainRawFilt = statement is not compatible with data.(dataNames{k}). You should change the lines to do all the checks at once:
if length(data.(dataNames{k})) == length(data.mainRawAlt)
MainRawFilt = data.mainRawAlt == 8888 ...
| data.mainRawAlt == 6000 ...
| (data.mainRawAlt > 5783) & (data.mainRawAlt < 5784);
data(MainRawFilt).(dataNames{k}) = [];
end

Voss
Voss on 16 Jul 2022
I think the syntax you want is like this:
data.(dataNames{k})(MainRawFilt) = [];
  2 Comments
Voss
Voss on 16 Jul 2022
You're welcome!
Let me offer some explanation of the difference between how you had it and how it should be:
data(MainRawFilt).(dataNames{k}) = [];
% ^^^^^^^^^^^^^ index data with MainRawFilt
data.(dataNames{k})(MainRawFilt) = [];
% ^^^^^^^^^^^^^ index data.(dataNames{k}) with MainRawFilt
In the first case, you are asking MATLAB to do something with data(MainRawFilt), i.e., since MainRawFilt is a logical array, data(MainRawFilt) is the elements of data where MainRawFilt is true. But data is (or appears to be) a scalar structure (it's only one structure, not an array of structures), so it doesn't have more than one element - its size doesn't match the size of MainRawFilt, so that's the source of the error.
In the second case, you are asking MATLAB to do something with data.(dataNames{k}), i.e., the field called dataNames{k} in the structure data. The value of this field is an array (of the appropriate length, based on the check that was done previously in the code), so that's the thing you actually want to be doing the logical indexing on, using MainRawFilt.
Let me know if you have any questions. Otherwise, please "Accept This Answer". Thanks!

Sign in to comment.

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!