Delete empty field - rows in a structure

I have a structure which has some empty field - rows. How can, I delete these empty them from the structure?

댓글 수: 6

Guillaume
Guillaume 2019년 7월 25일
편집: Guillaume 2019년 7월 25일
A structure array cannot have empty rows. Some fields of a scalar or array structure may have empty rows but if that's what you're talking about, we need to know more about the structure. Is it a scalar structure or a structure arrays? Is all fields or just one field that has empty rows? and If it's only one of the field, what should happen to the other fields?
An example of your structure would really help.
SS
SS 2019년 7월 25일
Hi. Thanks for your reply. It is an structure array and all the fields for some Structure(i) are empty. I want to delete the whole row from the structure. I am attaching the picture for the clarity. Structure.png
Track_20( all( cell2mat( arrayfun( @(x) structfun( @isempty, x ), Track_20, 'UniformOutput', false ) ), 1 ) ) = [];
would work, I think, though it looks ghastly. I'm sure there is probably a simpler way!
SS
SS 2019년 7월 25일
Awesome, it worked for me.
Thank you so much.
Stephen23
Stephen23 2019년 7월 25일
"I want to delete the whole row from the structure"
Your structure only has one row (your screenshot shows that it has size 1x935).
Do not confuse the size of the structure itself with the sizes of its fields, as Guillaume already explained.
Guillaume
Guillaume 2019년 7월 25일
Yes, the structure has only one row and 935 columns and it's the columns you want deleted. When the structure array is a vector matlab displays it in a tabular format along the rows regardless of whether it's a row vector or a column vector.
You may want to consider using a table instead of a structure. This would be a lot easier for you to manipulate. I'll write that as an answer.

댓글을 달려면 로그인하십시오.

 채택된 답변

Adam
Adam 2019년 7월 25일

6 개 추천

Added as an answer since it seemed to solve the problem.
Track_20( all( cell2mat( arrayfun( @(x) structfun( @isempty, x ), Track_20, 'UniformOutput', false ) ), 1 ) ) = [];
would work, I think, though it looks ghastly. I'm sure there is probably a simpler way!

추가 답변 (2개)

Andrei Bobrov
Andrei Bobrov 2019년 7월 25일
편집: Andrei Bobrov 2019년 7월 25일

7 개 추천

out = Track_20(all(~cellfun(@isempty,struct2cell(Track_20))));

댓글 수: 2

SS
SS 2019년 7월 25일
Thank you.
Hamza Imtiaz
Hamza Imtiaz 2020년 9월 21일
This is the correct answer

댓글을 달려면 로그인하십시오.

Guillaume
Guillaume 2019년 7월 25일
편집: Guillaume 2019년 7월 25일

2 개 추천

As commented, you may want to use a table instead of a structure. tables are a lot easier to use:
t_track20 = struct2table(track_20); %convert structure to table
%t_track20 = rmmissing(t_track20); %remove empty rows. All done!
edit: actually no, it's more complicated, as rmmissing doesn't work with cell arrays variables.:
toremove = rowfun(@(varargin) isempty([varargin{:}]), t_track20, 'ExtractCellContents', true);
t_track20(toremove, :) = [];
You may still want to use a table, generally it is easier to work with than structures. You can easily index table by row or by variables.

카테고리

도움말 센터File Exchange에서 Structures에 대해 자세히 알아보기

질문:

SS
2019년 7월 25일

댓글:

2020년 9월 21일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by