MATLAB: Problem when using system function

MATLABsystem

I used function system() to copy several files, and to deal with possible interaction, I also used the command 'yes'. The code I used is like
system('yes | cp -f filename destination')
The command works well in the terminal, but doesn't work when I run the above in matlab, which cannot return to the matlab process.
However, if I remove 'yes', the following works
system('cp -f filename destination')
Could someone please tell me what I should do to fix the first one and what the problem is? Thank you!
I use Matlab R2019a on macOS Catalina.

Best Answer

cp -f does not query, so using yes with it is not appropriate.
cp -i would be appropriate for querying, and using yes for that would be appropriate -- but you could just use the -f option instead.
Still, there are cases of utilities that query and which you do not reasonably have control over the source to modify the flag.
My tests show that the yes and the cp is being executed, that all the appropriate actions are taking place, but that for reasons I do not presently understand, the system() process is not exiting.
system() in the above case would execute
/bin/bash -c 'yes | cp -i filename destination'
My test is on OS-X High Sierra.
I do not have a work-around at the moment.
Related Question