I have a 32x1 struct array
. Each element of this array has several fields. I am trying to check which elements are empty (done), but then I want to create a vector with all the elements that were not empty.
For example I have the struct array
called satdata
, from which I want to see if the field SVID
is empty, so satdata(i).SVID
. SVID should go from 1 through 32 for my different elements. But if there is a missing element, then it can be [1:4 6:10 11:32]
. So I want to create a column vector that is (for this case)
[1;2;3;4;6;7;8;9;10;11;12;13;14;15;16;17;18;19...32].
This is what I have so far:
for i = 1:32
if isempty(satdata(i).SVID)
continue
else
svid = satdata(i).SVID;
svIdVec(i,:) = svid;
end
end
Which correctly checks for the empty slots, but when I create the vector I get something that looks like svIdVec = [0,2,3,4...32]
. In this case only the first element is empty. Therefore I want to get a 31x1
vector such as [2,3,4...32]
.
You don't need the loop:
svIdVec = {satdata.SVID};
[svIdVec{~cellfun(@isempty,svIdVec)}]'; % select only the non-empty values
If the SVID field is a vector and you want to aggregate that in a matrix, the following should be used (above code for scalars cannot be used to handle vectors):
svIdVec = {satdata.SVID}';
svid = ~cellfun(@isempty,svIdVec );
svIdVec = cell2mat(svIdVec(svid));