Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output Text to Octave Console

Lets say I have a variable A=5 and i want to output it, but with some text added in front and after it. Something like this: "There are 5 horses." (mind that 5 should be changable variable A)

If I write: disp("There are "),disp(A),disp(" horses.") I get:

There are 
5
 horses.

BUT I want everything in one line.

How do I do that?

like image 734
user1926550 Avatar asked Mar 07 '13 08:03

user1926550


2 Answers

You can use:

A = 5
printf("There are %d horses\n", A)

output:

There are 5 horses

or even

disp(["There are ", num2str(A), " horses"])

or even

disp(strcat("There are ", num2str(A), " horses"))

but you will have to add something because octave/matlab don't let the white space at the end of a string, so the output is:

ans = There are5 horses
like image 102
ThiS Avatar answered Sep 22 '22 07:09

ThiS


As per official documentation,

Note that the output from disp always ends with a newline.

so to avoid the newline, you should use an alternative to output data for each string, or first concatenate a single string and then disp it.

ThiS listed options.

like image 34
sancho.s ReinstateMonicaCellio Avatar answered Sep 18 '22 07:09

sancho.s ReinstateMonicaCellio