incl/Matlab.setFunction.R

code <- c(
  "function [win, aver] = dice(B)",
  "%Play the dice game B times",
  "gains = [-1, 2, -3, 4, -5, 6];",
  "plays = unidrnd(6, B, 1);",
  "win = sum(gains(plays));",
  "aver = win;"
)

setFunction(matlab, code)
evaluate(matlab, "[w, a] = dice(1000);")
res <- getVariable(matlab, c("w", "a"))
print(res)
HenrikBengtsson/R.matlab documentation built on Aug. 28, 2022, 3:21 p.m.