How to select non-string variables convertvars( ) argument?

3 ビュー (過去 30 日間)
Simon
Simon 2024 年 1 月 31 日
コメント済み: Simon 2024 年 2 月 4 日
I want to convert non-string variables to string using convertvars. I can do convertvars(T, @isnumeric, 'string') and convertvars(T, @iscell, 'string'). When I want to convert all variables to string, that approach would be cumbersome. I have tried ~@isstring. That is of course a rediculous and wrong idea. Is there a way to select non-string variables from a table?
c = {1,2,3;'a','b','c'; "d", "e", "f"}'
c = 3×3 cell array
{[1]} {'a'} {["d"]} {[2]} {'b'} {["e"]} {[3]} {'c'} {["f"]}
t = cell2table(c)
t = 3×3 table
c1 c2 c3 __ _____ ___ 1 {'a'} "d" 2 {'b'} "e" 3 {'c'} "f"
convertvars(t, @isnumeric, 'string') % convert one column at a time
ans = 3×3 table
c1 c2 c3 ___ _____ ___ "1" {'a'} "d" "2" {'b'} "e" "3" {'c'} "f"
convertvars(t, 1:width(t), 'string') % works but I like to see a better solution
ans = 3×3 table
c1 c2 c3 ___ ___ ___ "1" "a" "d" "2" "b" "e" "3" "c" "f"
  2 件のコメント
Mathieu NOE
Mathieu NOE 2024 年 1 月 31 日
why is
convertvars(t, 1:width(t), 'string') % works but I like to see a better solution
not a good solution in your eyes ?
Simon
Simon 2024 年 2 月 4 日
load patients.mat
t=table(Age, Location, LastName, Gender, Height);
This solution is slower because it converts string to string reduntly.
tic
convertvars(t, 1:width(t), 'string');
toc
Elapsed time is 0.016945 seconds.
Much faster solution
tic
convertvars(t, @(x)~isstring(x), 'string');
toc
Elapsed time is 0.004329 seconds.

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

採用された回答

Voss
Voss 2024 年 1 月 31 日
c = {1,2,3;'a','b','c'; "d", "e", "f"}'
c = 3×3 cell array
{[1]} {'a'} {["d"]} {[2]} {'b'} {["e"]} {[3]} {'c'} {["f"]}
t = cell2table(c)
t = 3×3 table
c1 c2 c3 __ _____ ___ 1 {'a'} "d" 2 {'b'} "e" 3 {'c'} "f"
convertvars(t, @(x)~isstring(x), 'string')
ans = 3×3 table
c1 c2 c3 ___ ___ ___ "1" "a" "d" "2" "b" "e" "3" "c" "f"

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by