3

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 3

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

You don't even need the explicit string(...) conversions. Most types will automatically be converted to strings when you do "some string" + x. So disp("a :: " + a + " :: b :: " + b) would work too.
2

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

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

Comments

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.