# display symbols between numbers from a vector

8 ビュー (過去 30 日間)
Vincent TORRELLI 2019 年 10 月 18 日
コメント済み: Vincent TORRELLI 2019 年 10 月 18 日
Hello,
I'm writing a code to help people on mental computation. To do so, I want to display a sum with the symbol "+" between two numbers located in a vector.
I know it should be simple but I did not succeed so far. Here is an example:
I want to display on command window:
>> 10 + 11
The values 10 and 11 are located in a vector such as:
calc=[10 11];
I tried to do the following code but it doesn't work:
sum_1= [calc(1)," + ", calc(2)];
disp(sum_1);
Can you help me please. Thank you very much.
Vincent
##### 1 件のコメント-1 件の古いコメントを表示-1 件の古いコメントを非表示
darova 2019 年 10 月 18 日
You should convert number into string
Use num2str

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

### 採用された回答

Ted Shultz 2019 年 10 月 18 日
you can use fprintf to print just about anything you want.
calc=[10 11];
fprintf('%i + %i\n', calc(1), calc(2))
##### 4 件のコメント2 件の古いコメントを表示2 件の古いコメントを非表示
Ted Shultz 2019 年 10 月 18 日
If you want spaces, you may want to just build up the line element by element. One way to do this would be:
a=[ 1 3 -4 5 -10];
workingString = sprintf('%i ',a(1));
for ii = 2:numel(a)
if a(ii) >= 0
thisSymbol = '+';
else
thisSymbol = '-';
end
workingString = [workingString sprintf('%s %i ',thisSymbol , abs(a(ii) ))]; %#ok<AGROW>
end
workingString = [workingString newline];
disp(workingString)
this gives:
1 + 3 - 4 + 5 - 10
Vincent TORRELLI 2019 年 10 月 18 日
Awesome, that's perfectly working.
Thank you very much !
Vincent

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

### その他の回答 (2 件)

Steven Lord 2019 年 10 月 18 日
Since you're using double quotes to create a string, turn your numeric vector into a string array then join the elements of that string array together.
calc = [10 11]
S = string(calc)
sum_1 = join(S, " + ")
You could do this in one line if you don't want to name the string temporary variable.
calc = [10 11]
sum_1 = join(string(calc), " + ")
Alternately if your calc vector is longer and you want to add different symbols you can use + to concatenate the string and numeric data together.
calc = 10:12;
sum1 = calc(1) + " + " + calc(2) + " * " + calc(3)
If this needs to run on an older release of MATLAB that doesn't support string, you can use sprintf.
calc = [10 11]
sum_1 = sprintf('%d + %d', calc)
##### 0 件のコメント-2 件の古いコメントを表示-2 件の古いコメントを非表示

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

Vincent TORRELLI 2019 年 10 月 18 日
calc = [10 11]
S = string(calc)
sum_1 = join(S, " + ")
Indeed, the calc vector is parameterized so that its size can vary from 1 to n.
The calc vector contains "n" random numbers that can be either positive and negative.
The final goal will be to not print a "+" sign if the number is negative:
what I don't want: -15 + -16 + 13
what I want: -15 -16 +13
Do you know how to do it?
Vincent

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

### カテゴリ

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