如何使Matlab捕获其中包含自变量的变量的输入名称? [英] How do I get Matlab to capture the input names of variables that have arguments inside of them?

查看:397
本文介绍了如何使Matlab捕获其中包含自变量的变量的输入名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

执行以下函数调用:

Corr1DTimeSeriesMap(NetClimatologySubtracted(:,:,WinterIndices), NAO(WinterIndices));

如果是Corr1DTimeSeriesMap(NetClimatologySubtracted,NAO);,则

inputname(1) = 'NetClimatologySubtracted'

inputname(2)= 'NAO'

但是一旦我仅选择WinterIndicesinputname就会消失. inputname对我来说很重要,因为我将它们用于我的图和将文件另存为的文件名.

But the inputnames disappear once I select for only the WinterIndices. The inputnames are important for me as I use them for both my plots and filenames that I save the files as.

推荐答案

您所看到的完全是预期的行为.如果通过

What you are seeing is completely expected behaviour. If you pass

NetClimatologySubtracted

然后按原样传递变量.但是,如果您通过

then you are passing the variable as-is. But, if you are passing

NetClimatologySubtracted(:,:,WinterIndices)

您正在传递一个未命名变量,该变量等于NetClimatologySubtracted的特定片段.完全合乎逻辑的是,没有名称的未命名变量.

you are passing an unnamed variable, equal to a specific slice of NetClimatologySubtracted. It's completely logical that there is no name for an unnamed variable.

您可以重新定义函数以使用三个参数:

You could re-define your function to expect three arguments:

Corr1DTimeSeriesMap(NetClimatologySubtracted, NAO, WinterIndices);

,然后在Corr1DTimeSeriesMap中进行切片.但是,正如鲍勃·吉尔莫尔(Bob Gilmore)所指出的,这有点臭.

and then take the slices inside Corr1DTimeSeriesMap. But, as already indicated by Bob Gilmore, this is a bit smelly.

将所有功能尽可能地彼此分离是一种更好的编程实践.就您而言,这意味着Corr1DTimeSeriesMap应该能够独立产生有意义的输出 您碰到的变量.

It is simply better programming practice to de-couple all your functions as much as possible from each other. In your case, that means that Corr1DTimeSeriesMap should be able to produce meaningful output independently of what variables you happen to throw in.

更好的方法将是通过允许明确指定标签名称来打破对变量名称的这种依赖:

A better approach by far would be to break this dependency on variable names by allowing explicit specification of the label names:

Corr1DTimeSeriesMap(...
    NetClimatologySubtracted(:,:,WinterIndices),...
    NAO(WinterIndices), ...
    {'Net climatology subtracted', 'NAO_{45}'});

它更强大,并且也可以使用更漂亮的标签:)

it's more robust, and allows for prettier labels too :)

标签(通常)已经是有效的文件名,但是要确保100%确定,可以将其与

Labels are (often) already valid filenames, but to make 100% sure, you could use this in conjunction with genvarname to generate valid filenames from labels specified thusly.

或者,您也可以将文件名作为附加参数:

Alternatively, you can also have the filenames as additional argument:

Corr1DTimeSeriesMap(...
    NetClimatologySubtracted(:,:,WinterIndices),...
    NAO(WinterIndices), ...
    {'Net climatology subtracted', 'NAO'},...
    {'NetClimSub.txt', 'NAO.txt'});

这篇关于如何使Matlab捕获其中包含自变量的变量的输入名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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