Skip to content

Commit a774ff6

Browse files
authored
Remove references to deprecated @matlab macro (#125)
Remove references to deprecated `@matlab` macro
1 parent 8ef1ecd commit a774ff6

File tree

1 file changed

+32
-40
lines changed

1 file changed

+32
-40
lines changed

README.md

Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,11 @@ This example will create a MAT file called ``test.mat``, which contains six MATL
228228
To evaluate expressions in MATLAB, one may open a MATLAB engine session and communicate with it. There are three ways to call MATLAB from Julia:
229229

230230
- The `mat""` custom string literal allows you to write MATLAB syntax inside Julia and use Julia variables directly from MATLAB via interpolation
231-
- The `@matlab` macro, in combination with `@mput` and `@mget`, translates Julia syntax to MATLAB
231+
- The `eval_string` evaluate a string containing MATLAB expressions (typically used with the helper macros `@mget` and `@mput`
232232
- The `mxcall` function calls a given MATLAB function and returns the result
233233

234+
In general, the `mat""` custom string literal is the preferred method to interact with the MATLAB engine.
235+
234236
*Note:* There can be multiple (reasonable) ways to convert a MATLAB variable to Julia array. For example, MATLAB represents a scalar using a 1-by-1 matrix. Here we have two choices in terms of converting such a matrix back to Julia: (1) convert to a scalar number, or (2) convert to a matrix of size 1-by-1.
235237

236238
##### The `mat""` custom string literal
@@ -253,49 +255,19 @@ mat"""
253255

254256
As with ordinary string literals, you can also interpolate whole Julia expressions, e.g. `mat"$(x[1]) = $(x[2]) + $(binomial(5, 2))"`.
255257

256-
##### The `@matlab` macro
257-
258-
The example above can also be written using the `@matlab` macro in combination with `@mput` and `@mget`.
259-
260-
```julia
261-
using MATLAB
262-
263-
x = linspace(-10., 10., 500)
264-
@mput x # put x to MATLAB's workspace
265-
@matlab plot(x, sin(x)) # evaluate a MATLAB function
258+
##### `eval_string`
266259

267-
y = linspace(2., 3., 500)
268-
@mput y
269-
@matlab begin
270-
u = x + y
271-
v = x - y
272-
end
273-
@mget u v
274-
@show u v
260+
You may also use the `eval_string` function to evaluate MATLAB code as follows
261+
```julia
262+
eval_string("a = sum([1,2,3])")
275263
```
276264

277-
###### Caveats of @matlab
278-
279-
Note that some MATLAB expressions are not valid Julia expressions. This package provides some ways to work around this in the ``@matlab`` macro:
280-
265+
The `eval_string` function also takes an optional argument that specifies which MATLAB session to evaluate the code in, e.g.
281266
```julia
282-
# MATLAB uses single-quote for strings, while Julia uses double-quote.
283-
@matlab sprintf("%d", 10) # ==> MATLAB: sprintf('%d', 10)
284-
285-
# MATLAB does not allow [x, y] on the left hand side
286-
x = linspace(-5.0, 5.0, 100)
287-
y = x
288-
@mput x y
289-
@matlab begin
290-
(xx, yy) = meshgrid(x, y) # ==> MATLAB: [xx, yy] = meshgrid(x, y)
291-
mesh(xx, yy, xx.^2 + yy.^2)
292-
end
293-
```
294-
295-
While we try to cover most MATLAB statements, some valid MATLAB statements remain unsupported by ``@matlab``. For this case, one may always call the ``eval_string`` function, as follows
296-
297-
```julia
298-
eval_string("[u, v] = myfun(x, y);")
267+
julia> s = MSession();
268+
julia> eval_string(s, "a = sum([1,2,3])")
269+
a =
270+
6
299271
```
300272

301273
##### `mxcall`
@@ -311,6 +283,26 @@ xx, yy = mxcall(:meshgrid, 2, x, y)
311283

312284
``mxcall`` puts the input arguments to the MATLAB workspace (using mangled names), evaluates the function call in MATLAB, and retrieves the variable from the MATLAB session. This function is mainly provided for convenience. However, you should keep in mind that it may incur considerable overhead due to the communication between MATLAB and Julia domain.
313285

286+
##### `@mget` and `@mput`
287+
288+
The macro `@mget` can be used to extract the value of a MATLAB variable into Julia
289+
```julia
290+
julia> mat"a = 6"
291+
julia> @mget a
292+
6.0
293+
```
294+
295+
The macro `@mput` can be used to translate a Julia variable into MATLAB
296+
```julia
297+
julia> x = [1,2,3]
298+
julia> @mput x
299+
julia> eval_string("y = sum(x)")
300+
julia> @mget y
301+
6.0
302+
julia> @show y
303+
a = 63.0
304+
```
305+
314306
#### Viewing the MATLAB Session (Windows only)
315307

316308
To open an interactive window for the MATLAB session, use the command `show_msession()` and to hide the window, use `hide_msession()`. *Warning: manually closing this window will result in an error or result in a segfault; it is advised that you only use the `hide_msession()` command to hide the interactive window.*

0 commit comments

Comments
 (0)