Search code examples
matlabdisp

How do you combine variables with strings in the disp method?


I want to print out some text and then a variable but I can't work out how to do it.

This is what I have at the moment:

disp('Temperature is:');
disp(UU(90));
disp('After: ');
disp(timeInMinutes);
disp('minutes');

but I'd like to have it all in a single disp.

I've tried using %f where the variables should be and then putting a comma and the variable name at the end as well as: disp('text ' + variable + 'more text');


Solution

  • You can concatenate your desired output into a single string inside the disp function using the square brackets [ ]. You will also need to convert your numbers to strings using the num2str function.

    Try this:

    disp(['Temperature is:' num2str(UU(90)) 'After: ' num2str(timeInMinutes) ' minutes']);