フィルターのクリア

how to change a cell in a string to a string in a table

77 ビュー (過去 30 日間)
tiwwexx
tiwwexx 2019 年 7 月 23 日
編集済み: Adam Danz 2020 年 12 月 13 日
Hey everyone, I'm trying to use the 'unique' matlab function.
unique(table.row)
When I try this I get the error "Cell array input must be a cell array of character vectors.". I think this is because my table is in the form
table.row(1) = {'string'}
But it needs to be in the form
table.row(1) = 'string'.
I tried to fix this by using the for loop below
for n= 1:numel(table.row)
table.row(n)=table.row{n};
end
but doing this I get the output error "Conversion to cell from char is not possible."
  2 件のコメント
Jon
Jon 2019 年 7 月 23 日
編集済み: Jon 2019 年 7 月 23 日
I understand that you are having a problem using the MATLAB unique function and you get the error message "Cell array input must be a cell array of character vectors" but I don't know what variable (argument) you are passing to the unique function. I see it has something to do with data that is in a table but it is not clear to me what is in your table or what you are passing to the unique function. Can you please be more specific. Best if you could please make a small, self contained example (very short script) that demonstrates the problem.
tiwwexx
tiwwexx 2019 年 7 月 23 日
Jon, I changed up the wording of the question so it has a more concrete example of the problem. Does this help clairify?

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

採用された回答

Adam Danz
Adam Danz 2019 年 7 月 23 日
編集済み: Adam Danz 2020 年 12 月 13 日
% This reproduces your error
T = table(num2cell(string(['a':'z']')),'VariableNames',{'row'});
unique(T.row) % ERROR!
% Convert from cellarray of strings vectors to string array
T = table(num2cell(string(['a':'z']')),'VariableNames',{'row'});
T.row = [T.row{:}]'; % *
unique(T.row) %class: string
% Do that twice to convert to characters
T = table(num2cell(string(['a':'z']')),'VariableNames',{'row'});
T.row = [T.row{:}]'; % *
T.row = [T.row{:}]'; % *
% Alternative: T.row = convertStringsToChars(T.row); % r2017b **
unique(T.row) %class: char
Notes
* if there is a missing value in T.row, this will break.
** Will not break if there are missing values in T.row
  2 件のコメント
tiwwexx
tiwwexx 2019 年 7 月 24 日
編集済み: tiwwexx 2019 年 7 月 24 日
Perfect! Just needed the convertStringsToChars / convertCharstoStrings. Thanks for the help.
Adam Danz
Adam Danz 2019 年 7 月 24 日
Nice! Thanks for the feedback!

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by