Removing items from a structure array in matlab

A. Masssey picture A. Masssey · Dec 4, 2012 · Viewed 14.8k times · Source

I have a very large structure array in matlab. Suppose, for argument's sake, to simplify the situation, I have something like:

structure(1).name = 'a';
structure(2).name = 'b';
structure(3).name = 'c';
structure(1).returns = 1;
structure(2).returns = 2;
structure(3).returns = 3;

Now suppose I have some condition that comes along and makes me want to delete everything from structure(2) (any and all entries in my structure array). What is a good way to do that?

My solution has been to just set the corresponding fields to [] (e.g. structure(1).name = [];), but that doesn't remove them, that only makes them empty. How do I actually remove them completely from the structure array? Is there a way?

Answer

Sameh Kamal picture Sameh Kamal · Dec 4, 2012

simple if you want to delete element at index i do the following:

i = 3
structure(i) = [];

And that will remove element at index 3.

Example:

st.name = 'text';
st.id = 1524;
arrayOfSt = [st st st st st];

Now:

arrayOfSt = 

    1x5 struct array with fields:
        name
        id

If we execute:

arrayOfSt(2) = [];

then the new value of the array of structers will be:

arrayOfSt = 

    1x4 struct array with fields:
        name
        id

Try it !