Calling Python function from MATLAB [duplicate] - python

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Call Python function from MATLAB
I need to call a Python function from a MATLAB environment. Is it possible?
Let's assume, I have the following Python code:
def squared(x):
y = x * x
return y
How do I call squared(3) from MATLAB workspace/code and get 9?

I have never done this before, so someone else will have to provide a 'real' answer, but
I can offer this.
I often call the shell from MATLAB on my Linux machine, with an exclamation point, >>!{comm}
So I suppose you could have a shell script that calls python and call the script from MATLAB.
wbg

Related

Does different python code execution implements different int arrays? [duplicate]

This question already has an answer here:
Different behavior in Python script and Python IDLE?
(1 answer)
Closed 9 months ago.
a = 1000
b = 1000
a is b
So if you run this code using pycharm or command python filename.py a is b = True
If you run this code using command line a is b = Fasle.
I know about implemented array in python (-5 .. 256), but the question is why it's True when you run your code not from command line?
Normally, a is b checks if both variables refer to the same object. And a == b checks for equality of their values. However, that might not be the case in python for small integers. Look at this reddit post with similar question to get more info.

Print statement as they are called in a python script automatically when the script runs [duplicate]

This question already has an answer here:
How can I decorate all functions imported from a file?
(1 answer)
Closed 4 years ago.
I'd like to automatically print python statements as they are called in a script while the script runs. Could anybody show a ready-to-use solution?
For example, I may have the following python code.
print "xxx"
myfun()
I'd like print "xxx" and myfun() be printed exactly in the output.
One solution is manually to add a print statement to each function. But this is combersome.
print 'print "xxx"'
print "xxx"
print 'myfun()'
myfun()
The following solutions just can not do so. Please do not mark this question as duplicate.
The solution here will not only print the functions in the current script but also functions in other scripts used in the current scripts. These solutions are not what I need.
How do I print functions as they are called
The following does not do what I need because it does not limit the decoration to the current file. For example, it uses import mymod1; decorate_all_in_module(mymod1, decorator). I don't have a module. I want to print the statements in the current script.
How can I decorate all functions imported from a file?
There is no "ready-to-use" solution to this problem. You are asking for a programming language with different semantics than the Python language: therefore you will either need to write code to get this effect, or use a different language, or both.
That said, for solving your actual problem, I believe simply using a debugger would do what you want: that way you can step over each statement in your program and observe as they are executed.

Is there a non-pdb way to enter Python interpreter at a given line? [duplicate]

This question already has answers here:
drop into python interpreter while executing function
(3 answers)
Closed 5 years ago.
Is there a way to cause a python script to enter the interpreter at a given line? I know I can use import pdb; pdb.set_trace() to begin debugging at a certain line, but that means all the pdb commands may get in the way.
Is there a way to kick into the regular interpreter (or ipython) outside of pdb?
The easiest way is code.interact:
import code
code.interact(local={**globals(), **locals()})

Python SyntaxError? [duplicate]

This question already has answers here:
What does "SyntaxError: Missing parentheses in call to 'print'" mean in Python?
(11 answers)
Closed 6 years ago.
I am learning a tutorial for Python language and trying to write a basic "Hello World!" program.
But when I do all steps described in the book I receive an error.
>> print "Hello World!"
SyntaxError: Missing parentheses in call to 'print'
Why I am getting this error?
Is my book wrong?
it seems that you're using Python 3.x.
In python 3.x, the print statement is a function and you need to use it as a function like this
print("Hello World!")
Your book is right, but might be outdated a bit. It seems it describes Python version 2, but you try to run your example on the version 3.
Python 3 has changed some features and this one is the most annoying to switch from P2 to P3.
"print" statement changed to function rather than operator as was in P2.
Calling function you should always use parentheses.
So, if you want to run your program in Python3 you should call it:
print("Hello World!")
And that's it.
If you want to use examples from your book as is - install Python2 and it should work.

How to open a python script with a constructor variable [duplicate]

This question already has answers here:
How to read/process command line arguments?
(22 answers)
Closed 8 years ago.
So i've been at this one for a little while and cant seem to get it. Im trying to execute a python script via terminal and want to pass a string value with it. That way, when the script starts, it can check that value and act accordingly. Like this:
sudo python myscript.py mystring
How can i go about doing this. I know there's a way to start and stop a script using bash, but thats not really what im looking for. Any and all help accepted!
Try the following inside ur script:
import sys
arg1 = str(sys.argv[1])
print(arg1)
Since you are passing a string, you need to pass it in quotes:
sudo python myscript.py 'mystring'
Also, you shouldn't have to run it with sudo.

Categories

Resources