How to concatenate strings and numbers with individual elements?

1 回表示 (過去 30 日間)
BHARAT CHARAN GOUD MARUPALLI
BHARAT CHARAN GOUD MARUPALLI 2021 年 1 月 12 日
編集済み: Walter Roberson 2021 年 1 月 12 日
I would like to concatenate the strings and numbers. If my input is Elements = {'H', 'N', 'Fe', 'Ni', 'Ti'} and Values = [1 1 0.3 0.5 0.2], I want the ouput of result = HNFe0.3Ni0.5Ti0.2. For the value '1', it should not assign anything.

採用された回答

Walter Roberson
Walter Roberson 2021 年 1 月 12 日
Elements = {'H', 'N', 'Fe', 'Ni', 'Ti'}
Elements = 1x5 cell array
{'H'} {'N'} {'Fe'} {'Ni'} {'Ti'}
Values = [1 1 0.3 0.5 0.2];
Vs = string(Values);
Vs(Values == 1) = "";
strjoin(Elements + Vs, '')
ans = "HNFe0.3Ni0.5Ti0.2"
  2 件のコメント
BHARAT CHARAN GOUD MARUPALLI
BHARAT CHARAN GOUD MARUPALLI 2021 年 1 月 12 日
Thank you so much Walter Roberson. Can you please suggest me how can I write the output in a statement form. For this I need the output as "The free energy of HNFe0.3Ni0.5Ti0.2 is 10KJ". The value '10' is assigned to the variable result (result = 10).
Walter Roberson
Walter Roberson 2021 年 1 月 12 日
編集済み: Walter Roberson 2021 年 1 月 12 日
"The free energy of " + strjoin(Elements + Vs, '') + " is" + result + "KJ"

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by