1

I am trying to run a simple Powershell script through Matlab. Its purpose is to exchange files between my laptop and HPC. From the command line everything works well, I can see files being copied and the whole process is finished in seconds.

powershell.exe -ExecutionPolicy Unrestricted -File C:\Users\...\TEST.ps1

But when I try to run in from Matlab, although I can still see the files being copied successfully, Matlab doesnt "stop", meaning it keeps busy forever and I need to shut it through Ctrl-C.

system('powershell.exe -ExecutionPolicy Unrestricted -File C:\Users\...\TEST.ps1')

As it is supposed to be a part of a bigger Matlab code, I would like it to work smoothly. Could anyone help me?

A.

Agnieszka
  • 61
  • 3
  • 1
    Does it keep busy while the files are being copied, or also after the copying has finished? – Luis Mendo Aug 02 '16 at 10:58
  • 1
    After the files being copied, until I kill it with Ctrl-C. And this is the problem. I understand that it keeps busy while the process is running, but Matlab keeps busy forever. – Agnieszka Aug 02 '16 at 12:23
  • I am still stuck with this problem, but I discovered that after the Powershell script is run successfully (all the files are copied) I can move on with Matlab by just pressing Enter. It still doesn't solve my problem, because I want it to be a part of a bigger code. – Agnieszka Aug 03 '16 at 10:31
  • If that works for you, you may want to [programmatically press Enter](http://stackoverflow.com/questions/27933270/programmatically-press-an-enter-key-after-starting-exe-file-in-matlab/27933690#27933690). But I guess Matlab can't do that if you don't go back to Matlab in the first place... Have you tried adding a `&`? That is, `system('powershell.exe -ExecutionPolicy Unrestricted -File C:\Users\...\TEST.ps1 &')` – Luis Mendo Aug 03 '16 at 10:34
  • I have tried the `&` solution and it sort of works. Meaning Matlab is not "stuck" anymore, but I have a command window popping up. I have found yet another solution, which works exactly how I wanted. I am going to post it as an answer. – Agnieszka Aug 03 '16 at 12:20

1 Answers1

0

I have found a solution. It works exactly how I wanted. Probably it is not the simplest one, but I will use it until I find something better:

   f1name = tempname;
   while exist(f1name,'file')
   f1name = tempname;
   end
   fid = fopen(f1name, 'wt');
   [a,b]=system(['powershell.exe -ExecutionPolicy Unrestricted -File C:\Users\...\TEST.ps1 < ' f1name]);
Agnieszka
  • 61
  • 3