How can I add [ ] to my strings within my cell array?

3 ビュー (過去 30 日間)
L
L 2017 年 1 月 24 日
コメント済み: Jan 2017 年 1 月 25 日
Hello,
So I've been trying to manipulate a cell array so that it is in the proper format for the datainsert command I am trying to use. An example would be, that I need something like:
columns = {'Name','Location','ID','Number'};
to be modified to become:
columns = {'[Name]','[Location]','[ID]','[Number]'};
I've tried messing with strjoin and join, but I haven't been able to figure it out. I'm sure it is something easy, I hope someone can provide an answer.
Additionally, my main goal is to create something that can be used for when my cell array contains 1Xn number of strings, as what I am using the master script for can have a different amount of things inside the cell array and I want to be able to add the [] to everything inside of it.
If something isn't clear let me know and I'll try and be more specific.
Thank you,
L

採用された回答

Walter Roberson
Walter Roberson 2017 年 1 月 24 日
columns = regexprep(columns, '(.*)', '[$1]');
  1 件のコメント
L
L 2017 年 1 月 25 日
Exactly what I was looking for. Thanks!

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

その他の回答 (1 件)

Jan
Jan 2017 年 1 月 25 日
columns = {'Name','Location','ID','Number'};
columns = strcat('[', columns, ']');
  2 件のコメント
L
L 2017 年 1 月 25 日
Thanks Jan! Walter posted first though and his answer worked.
Jan
Jan 2017 年 1 月 25 日
You are welcome. I'd prefer strcat, because it is faster. But in many cases the runtime does not matter.

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

カテゴリ

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