Class: Matlab::Engine
- Inherits:
-
Object
- Object
- Matlab::Engine
- Defined in:
- lib/matlab/engine.rb
Overview
The Engine class encapsulates a single connection to a MATLAB instance. Usage:
require 'matlab'
engine = Matlab::Engine.new
engine.put_variable "x", 123.456
engine.put_variable "y", 789.101112
engine.eval "z = x * y"
p engine.get_variable "z"
engine.close
# May also use block syntax for new
Matlab::Engine.new do |engine|
engine.put_variable "x", 123.456
engine.get_variable "x"
end
Values are sent to and from MATLAB by calling a method on the engine with the variable name of interest.
Instance Attribute Summary collapse
-
#driver ⇒ Object
readonly
A reference to the underlying MATLAB driver used by this engine.
-
#handle ⇒ Object
readonly
The low-level opaque engine handle that this object wraps.
Instance Method Summary collapse
-
#close ⇒ Object
Closes this engine.
-
#eval_string(string) ⇒ Object
Sends the given string to MATLAB to be evaluated.
-
#get_variable(name) ⇒ Object
Get a value from MATLAB via a given name.
-
#initialize(command = "matlab -nodesktop -nosplash", options = {}) ⇒ Engine
constructor
Create a new Engine object that connects to MATLAB via the given command.
-
#method_missing(method_id, *args) ⇒ Object
Call a MATLAB function passing in the arguments.
-
#put_variable(name, value) ⇒ Object
Put a value to MATLAB via a given name.
Constructor Details
#initialize(command = "matlab -nodesktop -nosplash", options = {}) ⇒ Engine
Create a new Engine object that connects to MATLAB via the given command
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/matlab/engine.rb', line 33 def initialize(command = "matlab -nodesktop -nosplash", = {}) load_driver([:driver]) @handle = @driver.open(command) if block_given? begin yield self ensure close end end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_id, *args) ⇒ Object
Call a MATLAB function passing in the arguments
63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/matlab/engine.rb', line 63 def method_missing(method_id, *args) method_name = method_id.id2name variable_names = [] args.each_with_index do |arg, index| variable_names << variable_name = "mr#{index}_#{method_name}" put_variable(variable_name, arg) end eval_string("#{method_name}(#{variable_names.join(", ")})") result = get_variable("ans") eval_string("clear #{variable_names.join(" ")}") result end |
Instance Attribute Details
#driver ⇒ Object (readonly)
A reference to the underlying MATLAB driver used by this engine.
30 31 32 |
# File 'lib/matlab/engine.rb', line 30 def driver @driver end |
#handle ⇒ Object (readonly)
The low-level opaque engine handle that this object wraps.
27 28 29 |
# File 'lib/matlab/engine.rb', line 27 def handle @handle end |
Instance Method Details
#close ⇒ Object
Closes this engine
79 80 81 |
# File 'lib/matlab/engine.rb', line 79 def close @driver.close(@handle) end |
#eval_string(string) ⇒ Object
Sends the given string to MATLAB to be evaluated
48 49 50 |
# File 'lib/matlab/engine.rb', line 48 def eval_string(string) @driver.eval_string(@handle, string) end |
#get_variable(name) ⇒ Object
Get a value from MATLAB via a given name
58 59 60 |
# File 'lib/matlab/engine.rb', line 58 def get_variable(name) @driver.get_variable(@handle, name) end |
#put_variable(name, value) ⇒ Object
Put a value to MATLAB via a given name
53 54 55 |
# File 'lib/matlab/engine.rb', line 53 def put_variable(name, value) @driver.put_variable(@handle, name, value) end |