フィルターのクリア

How can I assign the value of a variable out of other variables

1 回表示 (過去 30 日間)
Chewy
Chewy 2015 年 9 月 21 日
コメント済み: Walter Roberson 2015 年 9 月 21 日
I have the following three variables:
p_g = input( 'k or l', 's');
p_a = input( 'v or s', 's');
p_n = input( 'Enter number', 's');
The value of the forth variable shall be build out of the other variables:
p_k = ['p_g''p_a''p_n']
For example:
p_g = 'k'
p_a = 'v'
p_n = '15'
p_k = kv15
  1 件のコメント
Walter Roberson
Walter Roberson 2015 年 9 月 21 日
When you write
p_k = kv15
do you mean that you want p_k to be assigned the string 'kv15' or do you mean that you want p_k to be assigned the content of the variable kv15 ?

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

採用された回答

Guillaume
Guillaume 2015 年 9 月 21 日
There are many ways to do this:
p_k = [p_g, p_a, num2str(p_n)]; %note the lack of quotes
Or my preference:
assert(mod(p_n, 1) == 0, 'p_n is not integer') %for the %d to work in sprintf, p_n must be integer
p_k = sprintf('%s%s%d', p_g, p_a, p_n);
And please, use more descriptive variable names.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeWhos についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by