You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This example will create a MAT file called ``test.mat``, which contains six MATLAB variables:
@@ -228,9 +228,11 @@ This example will create a MAT file called ``test.mat``, which contains six MATL
228
228
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:
229
229
230
230
- 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`
232
232
- The `mxcall` function calls a given MATLAB function and returns the result
233
233
234
+
In general, the `mat""` custom string literal is the preferred method to interact with the MATLAB engine.
235
+
234
236
*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.
235
237
236
238
##### The `mat""` custom string literal
@@ -253,49 +255,19 @@ mat"""
253
255
254
256
As with ordinary string literals, you can also interpolate whole Julia expressions, e.g. `mat"$(x[1]) = $(x[2]) + $(binomial(5, 2))"`.
255
257
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
-
@matlabplot(x, sin(x)) # evaluate a MATLAB function
266
-
267
-
y =linspace(2., 3., 500)
268
-
@mput y
269
-
@matlabbegin
270
-
u = x + y
271
-
v = x - y
272
-
end
273
-
@mget u v
274
-
@show u v
275
-
```
276
-
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:
258
+
##### `eval_string`
280
259
281
-
```julia
282
-
# MATLAB uses single-quote for strings, while Julia uses double-quote.
You may also use the `eval_string` function to evaluate MATLAB code as follows
261
+
```julia
262
+
eval_string("a = sum([1,2,3])")
293
263
```
294
264
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
-
265
+
The `eval_string` function also takes an optional argument that specifies which MATLAB session to evaluate the code in, e.g.
*Note:* Since MATLAB functions behavior depends on the number of outputs, you have to specify the number of output arguments in ``mxcall`` as the second argument.
311
283
312
-
``mxcall`` puts the input arguments to the MATLAB workspace (using mangled names), evaluates the function call in MATLAB, and retrievs 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.
284
+
``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.
285
+
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
0 commit comments