24 views (last 30 days)
Show older comments
puccapearl on 24 Apr 2024 at 22:04
Commented: Voss about 21 hours ago
Accepted Answer: Voss
I have a table (M) with 4 columns (columns named X, Y, Z,K) , there is a break between the data that loads as NaN.
I want place each data chunk into cells with all 4 columns (X, Y, Z,K).
Thank you!
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
Accepted Answer
Voss on 24 Apr 2024 at 22:22
Open in MATLAB Online
% making a table with some all-NaN rows:
Var = randi(100,12,4);
Var([3 9],:) = NaN;
M = array2table(Var)
M = 12x4 table
Var1 Var2 Var3 Var4 ____ ____ ____ ____ 7 12 6 2 80 39 44 44 NaN NaN NaN NaN 48 78 45 83 3 97 37 62 35 55 21 4 37 44 22 79 85 95 92 85 NaN NaN NaN NaN 98 21 58 64 13 57 8 58 96 89 36 89
% split the table on the all-NaN rows into a cell array of tables:
idx = find(all(isnan(M{:,:}),2));
s_idx = [1; idx+1];
e_idx = [idx-1; size(M,1)];
result = arrayfun(@(s,e)M(s:e,:),s_idx,e_idx,'UniformOutput',false)
result = 3x1 cell array
{2x4 table} {5x4 table} {3x4 table}
celldisp(result)
result{1} = Var1 Var2 Var3 Var4 ____ ____ ____ ____ 7 12 6 2 80 39 44 44 result{2} = Var1 Var2 Var3 Var4 ____ ____ ____ ____ 48 78 45 83 3 97 37 62 35 55 21 4 37 44 22 79 85 95 92 85 result{3} = Var1 Var2 Var3 Var4 ____ ____ ____ ____ 98 21 58 64 13 57 8 58 96 89 36 89
10 Comments Show 8 older commentsHide 8 older comments
Show 8 older commentsHide 8 older comments
puccapearl on 24 Apr 2024 at 23:20
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/2111461-table-with-nans-into-separate-cells#comment_3142826
Thank you Voss, I now have a cell with tables. How can I pull each table that contains a specific column value. For example in the picture above, I want to pull all the tables where the 3rd column == 1 into a separate cell.
I tried but the tables combine as a double which I don't want, I want to keep the separate tables.
Can also repost as a different question.
Voss on 24 Apr 2024 at 23:41
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/2111461-table-with-nans-into-separate-cells#comment_3142836
Open in MATLAB Online
You're welcome!
One way to have all the tables in separate cells, but only the rows where column 3 is 1, is to split them first, as in my answer, and then:
result = cellfun(@(t)t(t{:,3} == 1,:),result,'UniformOutput',false);
puccapearl on 24 Apr 2024 at 23:54
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/2111461-table-with-nans-into-separate-cells#comment_3142846
amazing, thank you!
Voss on 25 Apr 2024 at 0:14
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/2111461-table-with-nans-into-separate-cells#comment_3142861
⋮
You're welcome!
puccapearl on 25 Apr 2024 at 0:23
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/2111461-table-with-nans-into-separate-cells#comment_3142866
Voss, one more thing, I want to subtract the first and last values of each table in column 1,
I tried this,
F = @(x)x(end)-x(1);
b = cellfun(F, result , 'un',0);
but I need to index into the first column of each table and I'm not sure how to do this.
Voss on 25 Apr 2024 at 0:54
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/2111461-table-with-nans-into-separate-cells#comment_3142876
F = @(x)x{end,1}-x{1,1};b = cellfun(F, result);
puccapearl on 25 Apr 2024 at 5:18
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/2111461-table-with-nans-into-separate-cells#comment_3142996
Edited: puccapearl on 25 Apr 2024 at 5:23
ah yes I thought so! My issue is, when I do this:
result = cellfun(@(t)t(t{:,3} == 1,:),result,'UniformOutput',false);
I get a cell with some empty tables,
and that gives me an indexing error error when I try to do:
F = @(x)x{end,1}-x{1,1};
b = cellfun(F, result);
Do you know how I can remove the empty tables prior to avoid the error? I tried:
result = table2cell(rmmissing(cell2table(result)))
and
result(cellfun(@isempty,result))=[];
and
result(:,all(ismissing(result)))=[];
and
new_result = rmmissing(result);
but they do not remove the empty tables :/
Voss about 23 hours ago
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/2111461-table-with-nans-into-separate-cells#comment_3143511
Open in MATLAB Online
This is how to do it. Try it again.
result(cellfun(@isempty,result)) = [];
puccapearl about 21 hours ago
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/2111461-table-with-nans-into-separate-cells#comment_3143721
It works! Thank you Voss! :D
Voss about 21 hours ago
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/2111461-table-with-nans-into-separate-cells#comment_3143736
You're welcome!
Sign in to comment.
More Answers (0)
Sign in to answer this question.
See Also
Categories
MATLABLanguage FundamentalsMatrices and Arrays
Find more on Matrices and Arrays in Help Center and File Exchange
Tags
- tables
- cells
- nan
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)
Europe
- 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