How can I convert table of chars to array of strings?

944 ビュー (過去 30 日間)
omar khater
omar khater 2019 年 8 月 7 日
コメント済み: omar khater 2019 年 8 月 8 日
Hello,
I have table variable (Input_table) like this
''1'' ''8'' ''2'' ''5'' '1' '1' ''Z'' ''F'' ''1'' ''3'' ''C'' ''5'' ''Y'' ''U'' ''4'' ''Q'' ''3''
''4'' ''4'' ''0'' ''9'' '1' '8' ''B'' ''R'' ''1'' ''2'' ''E'' ''8'' ''1'' ''Z'' ''1'' ''N'' ''X''
I want to convert every row into one string so the output to be array of 2 rows
'182511ZF13C5YU4Q3'
'440918BR12E81Z1NX'
I have tried
join(char(table2cell(Input_table))
but I get the error
Error using char
Cell elements must be character arrays.
  6 件のコメント
Guillaume
Guillaume 2019 年 8 月 7 日
Can you use valid matlab syntax to show how to create an example of your table, or attach your table as a mat file. Right now, it's very unclear how the data is stored in your table.
Perhaps, you should also explain how the table was created in the first place.
omar khater
omar khater 2019 年 8 月 7 日
編集済み: omar khater 2019 年 8 月 7 日
I imported the data from an excel sheet as categorical array. It was somewhat manual to work to create my table
I attached portion of my table

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

採用された回答

Guillaume
Guillaume 2019 年 8 月 7 日
I imported the data from an excel sheet as categorical array. It was somewhat manual to work to create my table
Yes, you made a complete mess of it. And you would be better off asking a new question on how to import the data correctly in the first place, rather than attempting to fix the mess afterward.
All the table variables are indeed categorical. Some of the categories have the quote ' embedded in the category (that is the ' you see when looking at the table is not the ' that matlab typically surround char arrays with, it's actual part of the data), others do not.
The simplest way to fix the mess after the fact:
join(erase(string(Exampletable{:, :}), "'"), '', 2)
which:
  • extracts the content of the table as a categorical array
  • converts the categorical array into a string array
  • erases the ' from the string array
  • joins the string array across the column
But again, a better approach would be to import the data correctly in the first place, so ask a new question.
  1 件のコメント
omar khater
omar khater 2019 年 8 月 8 日
Thank you for your answer. your line of code works fine as I want.
Returning to your point:
My problem is I have some dataset that I need to apply regression on it. My model works fine with the dataset imported from the excel so I don't think it has a problem. My problem was I need to convert back and forth between the string and table frequently so that I tried to make this function.

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

その他の回答 (2 件)

Azzi Abdelmalek
Azzi Abdelmalek 2019 年 8 月 7 日
s={'1' '8' '2' '5' '1' '1' 'Z' 'F' '1' '3' 'C' '5' 'Y' 'U' '4' 'Q' '3'
'4' '4' '0' '9' '1' '8' 'B' 'R' '1' '2' 'E' '8' '1' 'Z' '1' 'N' 'X' }
a=cellstr(reshape([s{:}],size(s)))
  1 件のコメント
omar khater
omar khater 2019 年 8 月 7 日
it works with direct cell arrays but doesn't work with table
whe I tried
b = table2cell(Input_table);
a=cellstr(reshape([b{:}],size(b)))
the output was:
2×17 cell array
Columns 1 through 5
{''1''} {''8''} {''2''} {''5''} {'1'}
{''4''} {''4''} {''0''} {''9''} {'1'}
Columns 6 through 10
{'1'} {''Z''} {''F''} {''1''} {''3''}
{'8'} {''B''} {''R''} {''1''} {''2''}
Columns 11 through 14
{''C''} {''5''} {''Y''} {''U''}
{''E''} {''8''} {''1''} {''Z''}
Columns 15 through 17
{''4''} {''Q''} {''3''}
{''1''} {''N''} {''X''}
which is not what I want unfortunately

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


Azzi Abdelmalek
Azzi Abdelmalek 2019 年 8 月 7 日
編集済み: Azzi Abdelmalek 2019 年 8 月 7 日
s={'1' '8' '2' '5' '1' '1' 'Z' 'F' '1' '3' 'C' '5' 'Y' 'U' '4' 'Q' '3'
'4' '4' '0' '9' '1' '8' 'B' 'R' '1' '2' 'E' '8' '1' 'Z' '1' 'N' 'X' }
a=join(s,2)
You can add a delimiter
a=join(s,'',2)
  1 件のコメント
omar khater
omar khater 2019 年 8 月 7 日
With this code i get the nearest solution so far
b = table2cell(Input_table);
a=cellstr(reshape([b{:}],size(b)));
d=join(a,2)
which is
d =
2×1 cell array
{''1' '8' '2' '5' 1 1 'Z' 'F' '1' '3' 'C' '5' 'Y' 'U' '4' 'Q' '3''}
{''4' '4' '0' '9' 1 8 'B' 'R' '1' '2' 'E' '8' '1' 'Z' '1' 'N' 'X''}
but I want to conectnate all row into 1 string

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

カテゴリ

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

タグ

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by