MATLAB和单元阵列处理的循环 [英] MATLAB and cell array handling in for loop

查看:120
本文介绍了MATLAB和单元阵列处理的循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的MATLAB和想提取单元阵列,我从我的数据库得到的数据:

I am new to MATLAB and would like to extract data from a cell array that I got from my database:

sensors = 

[ 1]    [23]    [1]    [  0]    [0.1000]            [1x29 char]
[ 2]    [23]    [1]    [120]    [0.1000]            [1x43 char]
[ 3]    [23]    [1]    [120]    [0.1000]            [1x42 char]
[ 4]    [23]    [1]    [ 15]    [0.1000]    'Air Temp Grey Box'
[ 5]    [23]    [1]    [120]    [0.1000]            [1x34 char]
[ 6]    [23]    [1]    [120]    [0.1000]            [1x33 char]
[ 7]    [23]    [1]    [120]    [0.1000]    'Pool Water Temp'  
[ 8]    [23]    [2]    [  0]    [0.1000]            [1x28 char]
[ 9]    [23]    [1]    [ 30]    [0.1000]            [1x22 char]
[10]    [23]    [1]    [ 30]    [0.1000]            [1x22 char]
[11]    [23]    [1]    [ 30]    [0.1000]            [1x21 char]
[12]    [23]    [1]    [ 15]    [0.1000]            [1x20 char]
[13]    [23]    [1]    [ 15]    [0.1000]            [1x23 char]
[14]    [23]    [1]    [ 30]    [0.1000]            [1x22 char]
[15]    [23]    [1]    [ 15]    [0.1000]    'Ground Air '      
[16]    [23]    [1]    [  5]    [0.1000]    'Boiler Cold Water'
[17]    [23]    [1]    [  5]    [0.1000]    'Boiler Hot Water' 
[18]    [23]    [1]    [  5]    [0.1000]    'Boiler CH Flow'   
[19]    [23]    [1]    [  5]    [0.1000]    'Boiler CH Return' 

现在我想抢到第一栏,即数字1到19,以及在最后一列中的相应名称,并在它们用于循环,例如:

Now I would like to grab the first column, i.e. the numbers 1 to 19 as well as the respective names in the last column and use them in a for loop, e.g.:

for ID=xxxx
    str = num2str(ID);
    SQLcommand = strcat('SELECT FROM data where ID=',str);
    answer = database.exec(SQLcommand);
    ......
end

我尝试了好几种不同的尝试,但从未成功地让刚刚的要素之一。

I have tried several different attempts but never succeeded in getting just one of the elements.

帮助是AP preciated :),在此先感谢。
轴突

Help is appreciated :), thanks in advance. axon

推荐答案

虽然圣人的回答上面会的工作,它不是真正正确的,或者有效地利用Matlab的电池阵列。您可以通过使用合适的单元阵列内容索引消除许多外来的函数调用。你可以解决两个方面单元阵列中的任何元素 - () {} ()获取细胞,还是作为一个单元。 {} 然而,翻出了单元格的内容,在它的基本类型。

Though sage's answer above will work, it's not really proper, or efficient use of Matlab's cell arrays. You can eliminate many of the extraneous function calls by using proper cell array content indexing. you can address any element of a cell array in two ways - () or {}. () gets the cell, still as a cell. {} however, pulls out the contents of the cell, in it's base type.

所以传感器(1月底)是1x1单元阵列,但传感器{1,结束} 是一个1x29字符的字符串。

So sensors(1, end) is a 1x1 cell array, but sensors{1, end} is a 1x29 char string.

有关你的问题:

numRows = size(sensors, 1);
for rowIdx = 1:numRows;
    sensorName = sensors{rowIdx, end};
    sql = ['select * from data where ID = ' num2str(sensors{rowIdx, 1})];
    ...
end

您也可以消除 num2str()打电话,如果你获取的传感器ID为char,而不是一个数字 - 即,如果你原来的数据库获取该填充传感器做了投。

You could also eliminate the num2str() call if you fetched the sensor ID as a char instead of a number - i.e. if your original DB fetch that populated sensors did the cast.

另外,如果你没有进一步从数据库中查询,您可以矢量化这件事,但我怕我从我的Matlab的机器很远,所以我不能建立一个一次性的顶部我的头。

In addition, if you weren't further querying from the DB, you could vectorize this whole thing, but I'm afraid I'm away from my Matlab machine, so I can't build that one off the top of my head.

这篇关于MATLAB和单元阵列处理的循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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