フィルターのクリア

How to merge words in the cells to form a string

13 ビュー (過去 30 日間)
Basu Sau
Basu Sau 2019 年 9 月 8 日
編集済み: madhan ravi 2019 年 9 月 10 日
Hi
I stuck into a phase of my work where I am looking forward to merge the words staying in the cells to form a string. My scenerio is like following:
A=562833 x 1 cell containing these following cells:
z1.jpg
As an example,
A{1,1} is of size {2x1 cell} contains words bihdai and majesti
A{2,1} is of size {5x1 cell} contains words model,love,take,time and ur
z2.jpg
Z3.jpg
Z4.jpg
I am looking forward to merge the cells to form a string and final output would be like
A(1)=" bihdai majesti"
A(2)="model love take time ur"
When I am trying with cell2str or string() to solve this, I am getting following error as well:
Conversion from cell failed. Element 1 must be convertible to a string scalar.
I am attaching a samples of the variables
Would you please advise me how to solve this scenerio.
Thanks,
  2 件のコメント
madhan ravi
madhan ravi 2019 年 9 月 8 日
Could you attach the data as .mat file? What version are you using?
Basu Sau
Basu Sau 2019 年 9 月 8 日
編集済み: Basu Sau 2019 年 9 月 8 日
@Madhan
Hi. The size of the mat file is big. I attach few screenshots of the problem in my question. I am using r2019a version.

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

回答 (3 件)

Bruno Luong
Bruno Luong 2019 年 9 月 8 日
編集済み: Bruno Luong 2019 年 9 月 8 日
A={{"happy" "birdday"} {"I" "am" "a" "boy"} {"good" "boy"}};
S = strtrim(string(char(cellfun(@(c) sprintf('%s ',c{:}), A, 'unif', 0))))
  2 件のコメント
Basu Sau
Basu Sau 2019 年 9 月 8 日
編集済み: Basu Sau 2019 年 9 月 8 日
Bruno Hi
thanks for your suggestion. But after trying your code, the following error is showing:
Brace indexing is not supported for variables of this type. I have uploaded few screenshots of the workspace. Might be it will help you to suggest me the path.
Error in calculateWord_Counts>@(c)sprintf('%s ',c{:}) (line 22)
a_=strtrim(string(char(cellfun(@(c) sprintf('%s ',c{:}), A, 'unif', 0))));
Bruno Luong
Bruno Luong 2019 年 9 月 8 日
編集済み: Bruno Luong 2019 年 9 月 8 日
The screen shot cannot desribe accurately your data structure/class.
Beside I never use the workspace so I don't know how to read it.

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


madhan ravi
madhan ravi 2019 年 9 月 8 日
編集済み: madhan ravi 2019 年 9 月 8 日
cellfun(@(x) join(string(x)),A).'
  2 件のコメント
Basu Sau
Basu Sau 2019 年 9 月 8 日
編集済み: Basu Sau 2019 年 9 月 8 日
Hi Madhan
Its showing this error after running your code:
a=cellfun(@(x) join(string(x)),A).';
Non-scalar in Uniform output, at index 1, output 1.
Set 'UniformOutput' to false.
Later I set this in to your code. But ultimately it did not convert it back in the strings. it converts data type of cell to string. (As the cell size is huge originally, I transpose your result to see inside the variables).
a_=a';
whereas I am looking to these outputs
a{1,1} will contain "bihdai majesti"
a{2,1} will contain "model love take time ur"
madhan ravi
madhan ravi 2019 年 9 月 8 日
編集済み: madhan ravi 2019 年 9 月 10 日
>> A={'Will';'you';'make like easier';'by';'clear for Godsake'}
A =
5×1 cell array
{'Will' }
{'you' }
{'make life easier' }
{'by explaining' }
{'clearly for Godsake'}
>> join(string(A))
ans =
"Will you make life easier by explaining clearly for Godsake"
>>

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


Bruno Luong
Bruno Luong 2019 年 9 月 9 日
編集済み: Bruno Luong 2019 年 9 月 9 日
a_=a'; % then copy screen
followed by useless screen capture.
Please do
a__ = a(1:10);
save(' a__.mat','a__');
and attach the file a__.mat here for us to try.

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by