0

I've recently moved from matlab to python and I am really missing what matlab called scritps. A script is like a function that it doesn't have variables you input or output. Instead, it sees the workspace that you are calling it in, and then everything it creates is then accessible in the workspace after its run.

(Here is a video introducing the concept of matlab scripts)

For example if your workspace is innitially the objects a, b and c. If you wanted a function that gets d from a, b and c you could design it so you could call it as:

d = MyFunc(a,b,c)

Alternately if it was a script, you could call it with:

MyScript

This is useful- because if you have code that you want to segregate but run on the whole workspace you don't have to write

d = MyFunc1(a,b,c)
e = MyFunc2(a,b,c,d)
f = MyFunc2(a,b,c,d,e)

but you could instead write

MyScript1
MyScript2
Myscript3

[This is hugely useful for processing scientific data where you may have a big workspace with lots of variables- and you want to do lots of operations that take large numbers of these variables - but you want to segregate them into blocks of code that are called by a main file so its easier to read]

3
  • 2
    I use Visual Studio Code. It comes with an integrated Jupyter notebook which is similar to Matlab interactive shell. You can run scripts or code fragments with a feature called cells (similar to Matlab cells). Those will use variables from the interactive workspace. So I don't think anything is missing compared to Matlab. Commented Dec 16, 2020 at 11:11
  • 1
    As a MATLAB gal I understand very well what you want, but I'm not sure the Python people will understand it. I suggest you show some additional example code, and how it works. I.e. make a simple script (and perhaps explain that it's saved in a separate file). Then show how you can call it from a function (or another script), and show that you now have all the variables created in the script and can use them. Unfortunately I don't know enough Python to answer this myself. Commented Dec 16, 2020 at 11:48
  • 1
    Being familiar with Python but not with Matlab, this sounds vaguely like you want to package your Python code into a module. This is standard practice for reusable code, and well documented (though best practices have evolved significantly over time, so make sure you read something reasonably recent and official). Commented Dec 16, 2020 at 12:13

1 Answer 1

1

In order to matlab-like script run, from the main_file.py:

exec(open('MyScript1.py').read())

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.