Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing items from a structure array in matlab

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?

like image 811
A. Masssey Avatar asked Dec 04 '12 08:12

A. Masssey


1 Answers

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 !

like image 108
Sameh K. Mohamed Avatar answered Nov 10 '22 23:11

Sameh K. Mohamed