MATLAB:基于行数组的内容设置许多变量 [英] MATLAB: Setting many variables based on the contents of a row array

查看:72
本文介绍了MATLAB:基于行数组的内容设置许多变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试整理代码.我有一个包含5列的数组,每个列都分配给一个变量.目前,我使用:

I'm trying to tidy up my code. I have an array with 5 columns, each of which is assigned to a variable. At present, I use:

x = inputData(i,1);
y = inputData(i,2);
currentSampleTime = inputData(i,3);
velocityX = inputData(i,4);
velocityY = inputData(i,5);

我认为我可以通过执行以下操作来整理一些东西:

I thought I could tidy things up a bit by just doing the following:

[x y currentSampleTime velocityX velocityY] = inputData(i,:);

显然,这无效有效.我认为必须有一个优雅的解决方案?

Apparently this does not work. I presume there must be an elegant solution?

推荐答案

如果inputData是一个单元格数组,则可以执行以下操作:

If inputData was a cell array then you could do this:

[x y currentSampleTime velocityX velocityY] = deal(inputData{i,:});

但是,由于您要使用变量i为行建立索引,因此我可以假定它在for循环内吗?

However, since you are indexing the row with the variable i can I assume that this is inside a for loop?

如果是的话,我会在循环之前做以下事情吗?

If so I would just do the following before the loop?

x = inputData(:,1);
y = inputData(:,2);
currentSampleTime = inputData(:,3);
velocityX = inputData(:,4);
velocityY = inputData(:,5);

然后在循环内使用x(i) y(i)等.

Then just use x(i) y(i) etc inside of your loop.

...或根据生成inputData的方式,尝试在读取或创建inputData时创建必要的数组.

... or depending on how inputData is generated try to create the necessary arrays when reading in or creating inputData.

另外,就我个人而言,我不喜欢将i用作m代码中的变量,因为如果未正确初始化,它很容易与虚数混淆.

Also, on a personal note I don't like using i as a variable in m-code because it can easily get confused with the imaginary number if not properly initialized.

这篇关于MATLAB:基于行数组的内容设置许多变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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