Handling Missing values in cell array from Excel cell extraction

18 ビュー (過去 30 日間)
Ty Wa
Ty Wa 2024 年 12 月 5 日 21:42
コメント済み: Walter Roberson 2024 年 12 月 6 日 20:14
I am directly referencing this previous question. I believe it is a good response. However, the issue for me is that the cell array I have in matlab is a character array and cellfun is returning a cell array of vectors for each character. MATLAB was throwing an error about setting 'UniformOutput' to false, which I did, and is when I realized this code runs but is not what I need. I should keep it to true so as to properly index which cells to remove.
example cell character array
extractedData.partCategories = {'xyz'} {'xyz'} {'xyz'} {1x1 <missing>} {1x1 <missing>}
Code
% Extract data with type conversion and error handling
% % <custom code to extract from Excel % %
% Clean up missing values in string cell arrays
mask = cellfun(@ismissing, extractedData.partCategories, 'UniformOutput', false);
extractedData.partCategories(mask) = {[]};
Then mask variable outputs:
[false, false, false]
[false, false, false]
[false, false, false]
true
true
Which I need it to output:
false
false
false
true
true

採用された回答

Walter Roberson
Walter Roberson 2024 年 12 月 5 日 21:45
mask = cellfun(@(C)any(ismissing(C(:))), extractedData.partCategories, 'UniformOutput', false);
  2 件のコメント
Ty Wa
Ty Wa 2024 年 12 月 6 日 19:48
Hi, thank you! The following code precisely works.
before:
mask = cellfun(@ismissing, extractedData.partCategories, 'UniformOutput', false);
extractedData.partCategories(mask) = {[]};
after:
mask = cell2mat(cellfun(@(C)any(ismissing(C(:))), extractedData.partCategories, 'UniformOutput', false));
extractedData.partCategories(mask) = []; % using {[]} just makes empty matrix cells, using [] removes them
Walter Roberson
Walter Roberson 2024 年 12 月 6 日 20:14
mask = cellfun(@(C)any(ismissing(C(:))), extractedData.partCategories);
without the cell2mat should work

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

製品


リリース

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by