Delete empty field - rows in a structure

132 ビュー (過去 30 日間)
SS
SS 2019 年 7 月 25 日
コメント済み: Hamza Imtiaz 2020 年 9 月 21 日
I have a structure which has some empty field - rows. How can, I delete these empty them from the structure?
  6 件のコメント
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 日
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 日
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 日
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.
  1 件のコメント
Andrei Bobrov
Andrei Bobrov 2019 年 7 月 25 日
+1

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by