matlab: how to print string and variable inline

Viewed 2417

I am new to matlab.

I am used to python. In python i generally do the below to print text and string together

a=10
b=20
print("a :: "+str(a)+" :: b :: "+str(b))

In matlab we have to use sprintf and use formats. But is this python kind of printing possible in matlab with any way.

3 Answers

As of MATLAB R2016b you can write:

disp("a :: "+string(a)+" :: b :: "+string(b)))

You can also use the concatenation operator '[]':

disp (['a :: ' num2str(a) ' :: b :: ' num2str(b)])

The first example uses the string array while the second uses character array.

You can use disp function to print and num2str to convert number to string

disp (['a :: ' num2str(a) ' :: b :: ' num2str(b)]

Using formatted print(fprintf) you can do that:

name = 'Alice';   
age = 12;
fprintf('%s will be %d this year.\n',name,age);

Also you can use disp command.

Related