Can I use strcat or strrep to represent 'space bar' between string?

5 ビュー (過去 30 日間)
Ara
Ara 2015 年 2 月 15 日
編集済み: Thorsten 2015 年 2 月 16 日
I have a text such as 'The image is'. I want to insert 'space bar' between font to be this --> T h e i m a g e i s
I have tried
ans = strcat(answer,'');
and
new_ans=strrep(ans2, '+', '');
but it didn't work or it cannot add space bar into text. Thank you
  1 件のコメント
Stephen23
Stephen23 2015 年 2 月 15 日
編集済み: Stephen23 2015 年 2 月 15 日
Do not use ans as the name of your variable, as ans has a special meaning in MATLAB.

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

採用された回答

Stephen23
Stephen23 2015 年 2 月 15 日
編集済み: Stephen23 2015 年 2 月 15 日
This will insert a space character between each character of any string:
str = 'my cat kathy';
str(2,:) = ' ';
str = str(1:end-1);
  4 件のコメント
Ilham Hardy
Ilham Hardy 2015 年 2 月 16 日
Anyone care to explain this witchcraft? :)
Thorsten
Thorsten 2015 年 2 月 16 日
編集済み: Thorsten 2015 年 2 月 16 日
str = 'my cat kathy';
defines str, a 1x12 array of char.
str(2,:) = ' ';
adds a 2nd row just of spaces, such that str is now a 2x12 arraw of char.
str = str(1:end-1);
converts the matrix to a vector, similar to str(:), where Matlab goes down the columns, i.e., the first letter 'm' at 1,1 is taken, then the first space at 2,1, next the second letter 'y' at 1,2, then the second space at 2,2 and so one. Because there need to be no space at the end, this final space is not used, therefor the last index is end-1. Et Voilà! Très élégante.

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

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by