Can create new environment (like new.env() in R) in MATLAB?

4 views (last 30 days)
In R, you can create new environment by
.new_env = new.env()
as shown in here .
Can we do something similar in MATLAB?
I am trying to build different calculation environments so that I can run different m-scripts and save workspace in each environment sequentially.
  6 Comments
Stephen23
Stephen23 on 6 Jun 2021
Edited: Stephen23 on 6 Jun 2021
"If I did not include 'clc, clear' in program1 and program 2, this code will work, but I would like to include them at the beginning."
MATLAB does not have named environments like R. MATLAB scopes variables and functions by their location: https://www.mathworks.com/help/matlab/scope-variables-and-generate-names.html
Rather than writing scripts with very smelly and anti-pattern code, the best solution by far is to use functions.
Right now you have a simple choice:
  1. spend quite a lot of your own time by continuing to use scripts (ugh), fighting MATLAB by adding CLEAR (ugh ugh) at the top of every script you write, and hoping for some magical way of telling MATLAB to not do exactly what you just told it to do: https://en.wikipedia.org/wiki/XY_problem https://xyproblem.info
  2. use functions.
Roy0014
Roy0014 on 6 Jun 2021
Yes, you are right. I should use function in this case. Thank you very much for your reply.

Sign in to comment.

Answers (0)

Products


Release

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!