Jump to content
Pat O

Matlab Script Node doesn't update when .m file edited externally

Recommended Posts

I've been using the Matlab script node and always noticed something funny about how it handles scripts (.m files) that I had saved. I finally figured out yesterday what it was. If you edit the script in matlab and save it, LabVIEW won't see the changes. For example:

Create a file called test.m which just has:

a=a+1;

Next create a vi that has a Matlab Script Node with inputs and and outputs of "a" attached to a control and indicator respectively which only contains a script node with the code:

cd <whatever directory test.m was saved to>;

test

If you run this code with the control set to 0, the indicator will read 1 as expected.

Now, if you go edit the file test.m to a=a+2; (without closing your vi) and then rerun the vi, it will still give you 1, not 2. If you close the vi, then reopen it, it will give you 2 as expected.

Is there a way to generate new execution code for LabVIEW without closing the vi and reopening it.

Share this post


Link to post
Share on other sites

I've been using the Matlab script node and always noticed something funny about how it handles scripts (.m files) that I had saved. I finally figured out yesterday what it was. If you edit the script in matlab and save it, LabVIEW won't see the changes. For example:

Create a file called test.m which just has:

a=a+1;

Next create a vi that has a Matlab Script Node with inputs and and outputs of "a" attached to a control and indicator respectively which only contains a script node with the code:

cd <whatever directory test.m was saved to>;

test

If you run this code with the control set to 0, the indicator will read 1 as expected.

Now, if you go edit the file test.m to a=a+2; (without closing your vi) and then rerun the vi, it will still give you 1, not 2. If you close the vi, then reopen it, it will give you 2 as expected.

Is there a way to generate new execution code for LabVIEW without closing the vi and reopening it.

This is probably occurring because you have two instances of MATLAB open: the one you are editing, and the one LabVIEW is using to communicate with MATLAB and run the .m file. Try this:

1) Start with all applications closed.

2) Open the VI that has the Matlab Script Node and run it. You should notice a MATLAB Command Window in your taskbar as well.

3) Instead of double clicking test.m to open it, or opening MATLAB yourself to edit it, switch to the MATLAB Command Window and type 'open test.m' and hit enter. You should get test.m up in an editor.

4) Edit test.m to say a=a+2 as you mentioned above and save it.

5) Run the VI again.

Because you edited the .m file in the same instance that will execute it, MATLAB knows that it needs to recompile it before it runs it. Apparently MATLAB does some caching when it compiles scripts into runnable code, and multiple instances of MATLAB don't talk to each other about which cached versions have been invalidated and should be recompiled. Editing in the same instance that will be doing the execution should fix that for you.

I hope that helps.

  • Like 1

Share this post


Link to post
Share on other sites

Two years later...I found another way to handle this which is to put a

clear functions

command in a MATLAB Script Node at the start of the VI. This will force a recompile of the externally-edited .m file.

  • Like 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.