Octave printf does not output when followed by ginput

264 views Asked by At

I am trying to make a prompt for the user to select from the figure (plot). When I run it with the code below, the prompt doesnt display until i click on the figure, after which the prompt displays and the code continues. In fact, no call to printf (or disp) that is called before the ginput call displays until i select the figure.

printf("Select part\n"); % (disp also doesnt work properly)
[xinput,yinput] = ginput(1);

The purpose of the prompt is to alert the user to move to the figure, so naturally it needs to display before selecting the figure.

I can add an extra redundant input call between the two which forces the printf to display in the console. eg input("Press Enter"). but this is an inconvenient solution.

Strangely, if you run just the code above it does work normally. But when running in the remainder of the program it displays the issue. So it may be a difficult one to debug. Also, running it one line at a time in the full code using the debugger works properly, displaying the prompt before selecting the figure.

Just to add to the confusion. When running this part of the program in a loop, the first instance doesnt display the prompt correctly, but every other instance it works.

Thanks

EDIT: The following code reliably fails (for me) in the same way my full program fails; (edited again to simplify)

figure(1);
input_test = input("press 1: ");
switch input_test
  case 1
    while true
      clc;
      printf("Left click to get coords or right click to finish\n");
      [xinput,yinput,mouse_button] = ginput(1)
      if mouse_button == 3
        break
      endif
    endwhile
endswitch

It appears it has something to do with the line;

input_test = input("press 1: ");

If I replace this with

input_test = 1;

it works properly. I dont know what is the reason for this, and I cannot remove the input request from this location.

1

There are 1 answers

0
Dominic On

Thanks Roger, you were correct, I did find a solution.

Using

fflush(stdout)

before the 'ginput' call solves the problem.

Found this in the 'input' help;

"Because there may be output waiting to be displayed by the pager,
it is a good idea to always call 'fflush (stdout)' before calling
'input'.  This will ensure that all pending output is written to
the screen before your prompt."