MATLAB是否支持嵌套变量作用域? [英] Does MATLAB support nested variable scopes?

查看:116
本文介绍了MATLAB是否支持嵌套变量作用域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MATLAB可以定义类似于以下类似伪C的代码的变量吗?

Can MATLAB define variables like the following pseudo C-like code?

{  
    int a = 0;
    int b, c;
    {
       int a = 42; 
       b = a;
    }
    c = a;
}

我们可以使用嵌套函数来模拟".我们可以不使用函数来做到这一点吗?

We could use nested functions to 'simulate' this. Can we do this w/o using functions?

我之所以这样问,是因为有一些我想转换为MATLAB的C代码.我想知道如何将嵌套变量声明转换为MATLAB.

I asked this because there are some C code that I wanted to convert to MATLAB. I was wondering how to convert the nested variable declarations to MATLAB.

谢谢

推荐答案

您试图做的事情是直接不可能的.好消息是,这可能很好,因为很难读取和正确维护遍布各处具有类似名称的变量的代码.最简单的解决方案是重命名变量:

What you are trying to do is impossible directly. The good news is that this is probably fine, because it is difficult to read and properly maintain code that has similarly named variables all over the place. The simplest solution would be to rename the variables:

{  
    int a = 0;
    int b, c;
    {
       int a = 42; 
       b = a;
    }
    c = a;
}

(在MATLAB中)将变为:

would become (in MATLAB):

a = 0;
d = 42;
b = d;
c = a;

如果内部" a和外部" a在做不同的事情,则给它们指定不同的名称不会对您造成伤害,甚至可能在以后为某人节省维护的噩梦.

If "inner" a and "outer" a are doing different things, you will do no harm by giving them different names, and perhaps even save someone a maintenance nightmare later down the line.

这篇关于MATLAB是否支持嵌套变量作用域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆