You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Matlab中执行脚本不修改当前工作区变量并访问脚本变量的方法

Got it, let's break down what's going on here and fix your problem.

First, why your previous attempts didn't work:

  • evalin('myworkspace', 'myscript') failed because MATLAB doesn't let you reference arbitrary named workspaces like that. The valid arguments for evalin's workspace identifier are only 'base' (the command line workspace), 'caller' (the workspace of the function/script that called your code), or a function handle pointing to a specific function's workspace. So 'myworkspace' isn't a valid target here.
  • evalin('caller', 'myscript') modified your current workspace because 'caller' refers to the exact workspace you're trying to protect—running the script there means it can create/overwrite variables just like if you ran the script directly.

Now, here's the clean solution: use a helper function to run the script in an isolated workspace, then return all the script's variables as a struct. Since functions have their own private workspaces, running the script inside a function keeps your current workspace untouched, and we can package up the script's variables to send back to you.

Step 1: Create the helper function

Make a new file named executeScriptIsolated.m with this code:

function scriptVars = executeScriptIsolated(scriptName)
    % EXECUTESCRIPTISOLATED Runs a script without polluting your current workspace
    % Input:  scriptName - Name/path of the script to run (with or without .m)
    % Output: scriptVars - Struct containing all variables created by the script

    % Run the script in this function's private workspace (no impact on your current workspace)
    run(scriptName);
    
    % Grab all variable names from the function's workspace
    varNames = who;
    
    % Build a struct to hold the script's variables
    scriptVars = struct();
    for idx = 1:length(varNames)
        varName = varNames{idx};
        scriptVars.(varName) = evalin('caller', varName);
    end
end

Step 2: Use the helper function

In your current workspace (command line or another script/function), call it like this:

% Run your script without modifying current variables
scriptOutput = executeScriptIsolated('myscript');

% Access the script's variables through the struct
disp(scriptOutput.myScriptVariable1);
disp(scriptOutput.myScriptVariable2);

How this works

  • The helper function has its own isolated workspace, so when run(scriptName) executes, all the script's variables are created there instead of your current workspace.
  • We use who to get all variable names from the helper's workspace, then populate a struct with those variables to return to you.
  • Your original workspace stays completely untouched—no new variables added, no existing variables overwritten.

Note

If your script uses global variables, those will still affect the global workspace (since global variables are shared across all workspaces). If that's a concern, you'd need to modify the script to avoid global variables, or add code to the helper function to handle them (e.g., clear globals after execution, but be careful with that if other parts of your code rely on them).

内容的提问来源于stack exchange,提问作者Dims

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 10:00:24