First of all, hello !

I'm fairly new to all of this CNC things, and as I'm no English native, I will do my utmost to be precise and not confusing in what I will be saying.

Then let's begin.

I constructed a CNC machine from scratch in order for it to be used as a scanner. I use UGS and Grbl to make all of this work. The gist of it is for the CNC to respect a certain pattern that I load in Gcode.
But as it is a scanner, I have a sensor that moves with the machine, and follow the pattern. I also have predefined position in an excel file following the pattern.
To be crystal clear : Arduino is making the CNC move and is acquiring data. Matlab is in charge of taking this data and associate it with position in order to plot.

But, you now understand if you ever tried to do this that there is a kind of offset. To explain, when I launch the CNC, I also have to launch matlab at the same time, otherwise there will be an offset (the CNC will have already moved but the matlab program will still not have begun). So I was thinking of creating a if loop in the Gcode that goes like
'If [condition is true]
Make the pattern
Else
Do not move
'
A while loop might also be interesting
'while [ condition is false]
Do not move
'
But, to be honest, I have litterally no idea how to do that. Can I send a caracter from matlab to arduino, and then make the condition dependant from this caracter ?
For example: matlab sends "t" to arduino using serial.
And in the gcode :
'while [T NE t]
...
'
It would allow me to precisely time the data acquisition with my pattern.

Thanks for your attention, I'm open to any idea whatsoever, I'm just afraid that this is not possible.

Best regards.