Cell of Strings, using cell2mat gives Dimensions of arrays being concatenated are not consistent.
76 ビュー (過去 30 日間)
古いコメントを表示
I've got an excel document with lots of fancy editing that I'm trying to do processing on.
I've imported it as a table since rows may change over time and I'd like to be able to use row names to reference table data. I need to make some changes to the first column in order to use T.Properties.RowNames
Here's been the process so far, Import as table > grab the first column with table2array > this gives an array of cells > replace all the 0x0 char empty cells with a unique string > use cell2mat to convert to an array of strings > pass string array to T.Properties.RowNames
however, when I go to use cell2mat I get the error "Dimensions of arrays being concatenated are not consistent."
can't figure out what's causing it, I tried using which was a fix I found elsewhere to check for empty cells that may be the cause:
isEm = cellfun( @isempty, namerows ) ;
data = cell2mat( namerows(~isEm) ) ;
but the cell2mat function in this fix returns the same error
Any help would be appreciated
Here's my code:
%impot table, extract first column, convert to an array of cells
T = readtable('3-22 week 3.xlsx');
namerows = T(:,1);
namerows = table2array(namerows);
%array of numbers to be passed to sprintf to ensure each row has a unique
%name
nullname = 1:length(namerows);
%replace any unnamed rows with a string
for n = 1:length(namerows)
if (namerows{n,1} == "")
namerows{n,1} = sprintf('null%d',nullname(n))
end
end
%double check there are no empty cells
isEm = cellfun( @isempty, namerows ) ;
data = cell2mat( namerows(~isEm) ) ; %CODE FAILS HERE
namerows = cell2mat(namerows)
採用された回答
Voss
2022 年 4 月 14 日
I'm not sure why you want to use cell2mat to convert a cell array of character vectors to an array of strings. You can use string for that:
% cell array of character vectors:
namerows = repmat({'name'},10,1);
namerows{4} = ''; % some are empty
%replace any unnamed rows with a string
nullname = 1:length(namerows);
for n = 1:length(namerows)
if (namerows{n,1} == "")
namerows{n,1} = sprintf('null%d',nullname(n));
end
end
disp(namerows) % none are empty -> removing empties before cell2mat won't matter
try
namerows = cell2mat(namerows)
catch ME
disp(ME.message);
namerows = string(namerows)
end
cell2mat on a cell array of character vectors will attempt to concatenate the character vectors, so they have to have consistent sizes:
cell2mat({'this_';'is_ok'})
cell2mat({'this_';'is_not'})
2 件のコメント
Voss
2022 年 4 月 14 日
You're welcome!
If it had been a cell array of strings, you would've gotten a different error (but using string to convert to a string array would still work):
% cell array of *strings*:
namerows = repmat({"name"},10,1);
namerows{4} = ""; % some are empty
%replace any unnamed rows with a string
nullname = 1:length(namerows);
for n = 1:length(namerows)
if (namerows{n,1} == "")
namerows{n,1} = sprintf('null%d',nullname(n)); % this is a character vector
end
end
disp(namerows) % some strings, some character vectors
try
% the error you'd get here is:
% "All contents of the input cell array must be of the same data type."
namerows = cell2mat(namerows)
catch ME
disp(ME.message);
% this works the same whether it's a cell array of character vectors or
% strings (or some of both)
namerows = string(namerows)
end
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Data Type Conversion についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!