13 views (last 30 days)
Show older comments
Joel Schelander on 26 Mar 2021
-
-
Link
Direct link to this question
https://ms-intl.mathworks.com/matlabcentral/answers/784191-removing-empty-cells-from-cell-array
Commented: Joel Schelander on 26 Mar 2021
Accepted Answer: Stephen23
G is a cell array:
G={16x16 cell16x16 cell16x16 cell16x16 cell16x16 cell16x16 cell}
Each cell look like this: G{1}=[1 [] 3 ; 4 [] 7]; (but 16x16)
I want to remove these empty cells []. I have tried
index = cellfun(@isempty, G) == 0;
Gnew = G(index)
Without success. How can I solve this?
2 Comments Show NoneHide None
Show NoneHide None
Stephen23 on 26 Mar 2021
Direct link to this comment
https://ms-intl.mathworks.com/matlabcentral/answers/784191-removing-empty-cells-from-cell-array#comment_1419371
Open in MATLAB Online
"Each cell look like this: G{1}=[1 [] 3 ; 4 [] 7]; "
I doubt that, as numeric arrays must be rectangular and cannot contain "holes":
[1,[],3;4,[],7]
ans = 2×2
1 3 4 7
Please upload some sample data in a mat file by clicking on the paperclip button.
Joel Schelander on 26 Mar 2021
Direct link to this comment
https://ms-intl.mathworks.com/matlabcentral/answers/784191-removing-empty-cells-from-cell-array#comment_1419391
- G.mat
It is the output of several for loops, looks like this (see attached mat file)
Sign in to comment.
Sign in to answer this question.
Accepted Answer
Stephen23 on 26 Mar 2021
Edited: Stephen23 on 26 Mar 2021
Open in MATLAB Online
- G.mat
Most likely converting the nested cell array to numeric arrays is going to make processing your data easier:
S = load('G.mat');
GUD = S.GUD
GUD = 1×6 cell array
{16×16 cell} {16×16 cell} {16×16 cell} {16×16 cell} {16×16 cell} {16×16 cell}
GUD{1}
ans = 16×16 cell array
{0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {[ 4.1165]} {[ 5.8874]} {[ 5.8474]} {0×0 double} {[ 5.8874]} {[ 4.5514]} {[ 6.3844]} {0×0 double} {[ 5.8474]} {[ 6.3844]} {[ 4.4562]} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {[ 5.8874]} {[ 6.3844]} {0×0 double} {0×0 double} {[ 6.3844]} {[ 5.8474]} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {[ 5.8874]} {0×0 double} {[ 6.3844]} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {[ 6.3844]} {0×0 double} {[ 6.3844]} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {[ 5.8474]} {[ 6.3844]} {0×0 double} {0×0 double} {[ 6.3844]} {[ 6.3844]} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {[ 5.8874]} {[ 6.3844]} {0×0 double} {0×0 double} {[ 6.3844]} {[ 5.8474]} {[ 4.1165]} {0×0 double} {[ 5.8874]} {[ 5.8474]} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {[ 5.8874]} {0×0 double} {[ 4.2759]} {[ 5.8416]} {[ 5.8474]} {0×0 double} {[ 5.8416]} {[ 4.3722]} {[ 5.8874]} {0×0 double} {0×0 double} {[ 6.3844]} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {[ 6.3844]} {0×0 double} {0×0 double} {[ 5.8416]} {[ 5.8474]} {0×0 double} {[ 6.3844]} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {[ 6.3844]} {0×0 double} {[ 5.8416]} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {[ 5.8874]} {0×0 double} {[ 6.3844]} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {[ 6.3844]} {0×0 double} {[ 6.3844]} {[ 5.8874]} {0×0 double} {0×0 double} {[ 6.3844]} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {[ 6.3844]} {0×0 double} {0×0 double} {[ 5.8416]} {[ 4.5514]} {[ 5.8874]} {0×0 double} {[ 6.3844]} {[ 5.8874]} {[ 4.2759]} {0×0 double} {[ 5.8416]} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {[ 6.3844]} {[ 5.8416]} {0×0 double} {[ 4.7729]} {[ 6.3844]} {[ 6.3844]} {0×0 double} {0×0 double} {[ 6.3844]} {[ 5.8416]} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {[ 5.8474]} {[ 6.3844]} {0×0 double} {0×0 double} {[ 6.3844]} {[ 6.3844]} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {[ 5.8474]} {0×0 double} {[ 6.3844]} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {[ 6.3844]} {0×0 double} {[ 5.8416]} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {[ 6.3844]} {[ 6.3844]} {0×0 double} {0×0 double} {[ 6.3844]} {[ 5.8416]} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {[ 4.4562]} {[ 5.8474]} {[ 6.3844]} {0×0 double} {[ 5.8474]} {[ 4.3722]} {[ 5.8416]} {0×0 double} {[ 6.3844]} {[ 5.8416]} {[ 4.7729]} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double}
Convert:
for k = 1:numel(GUD)
idx = ~cellfun(@isempty,GUD{k});
tmp = zeros(size(GUD{k})); % or perhaps NAN.
tmp(idx) = [GUD{k}{idx}];
GUD{k} = tmp;
end
Checking:
GUD
GUD = 1×6 cell array
{16×16 double} {16×16 double} {16×16 double} {16×16 double} {16×16 double} {16×16 double}
GUD{1}
ans = 16×16
0 0 0 0 0 4.1165 5.8874 5.8474 0 5.8874 4.5514 6.3844 0 5.8474 6.3844 4.4562 0 0 0 0 0 0 0 0 0 0 5.8874 6.3844 0 0 6.3844 5.8474 0 0 0 0 0 5.8874 0 6.3844 0 0 0 0 0 6.3844 0 6.3844 0 0 0 0 0 5.8474 6.3844 0 0 6.3844 6.3844 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5.8874 6.3844 0 0 6.3844 5.8474 4.1165 0 5.8874 5.8474 0 0 0 0 5.8874 0 4.2759 5.8416 5.8474 0 5.8416 4.3722 5.8874 0 0 6.3844 0 0 0 0 0 0 0 0 6.3844 0 0 5.8416 5.8474 0 6.3844 0 0 0 0 0 6.3844 0 5.8416 0 0 0 0 0 0 0 0 0 0 5.8874 0 6.3844 0 0 0 0 0 6.3844 0 6.3844 5.8874 0 0 6.3844 0 0 0 0 0 0 0 0 6.3844 0 0 5.8416
If you really want to keep the (very inefficient and difficult to work with) nested cell arrays containing scalar numerics, then something like this:
for k = 1:numel(GUD)
idx = ~cellfun(@isempty,GUD{k});
GUD{k}(idx) = {0};
end
1 Comment Show -1 older commentsHide -1 older comments
Show -1 older commentsHide -1 older comments
Joel Schelander on 26 Mar 2021
Direct link to this comment
https://ms-intl.mathworks.com/matlabcentral/answers/784191-removing-empty-cells-from-cell-array#comment_1419716
That really worked! Thank you!
Sign in to comment.
More Answers (0)
Sign in to answer this question.
See Also
Categories
MATLABLanguage FundamentalsData TypesData Type Identification
Find more on Data Type Identification in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office