Search code examples
matlabmatlab-struct

Remove data from struct bigger than a certain value


I have a struct, that's a <1x1 struct>, and I'm trying to edit a field in the struct based on the values. The field is called GeoDist_Actual and the struct is called GeoDist_str. The field GeoDist_Actual is a <262792x1 double>, and this is the code I was trying to use in order to get rid of the values that are greater than 1.609344e+05.

i =1;
for i=i:size(GeoDist_str.GeoDist_Actual)
    if GeoDist_str.GeoDist_Actual(i,1 > 1.609344e+05
    GeoDist_str.GeoDist_Acutal(i,1) = [];
    end
end

How would I append or alter this code in order to make it function like I'm aiming? I considered setting all the values to 0, but I'm going to have to go backwards from this in order to get back GPS values, doing a reverse-Vincenty(spherical) calculation, and I'd like to just completely get rid of the values that don't comply with the if condition.

If I can narrow down the question at all, let me know, and thank you for your help in advance!

Edit: I've noticed that when I changed out the section

GeoDist_str.GeoDist_Actual(i,1) = []; 

for

GeoDist_str.GeoDist_Actual(i,1) = 0;

It didn't actually solve anything, instead it didn't access the field "GeoDist_Actual" within the struct "GeoDist_str", it just created a mirror field with values of 0.


Solution

  • Consider this example:

    % a 10-by-1 vector
    x = [1;2;3;4;5;6;7;8;9;10];
    
    % remove entries where the value is less than five
    x(x<5) = [];
    

    This is called logical indexing, no need for loops.