0

这很简单,但我在 Matlab 中处理数据类型时遇到了一些问题。

下面将创建一个简单的数组。

l=[]
for i=1:10
   # do something here
   # i = i* i;
end
plot(l) # would happily plot it

在下面的代码中,data从每行包含字符串的文件加载到工作区。每行被视为每一行,数据为String.

numbers=[]
index = 1;
split_data = regexp(data, ' ', 'split');
for eachLine=split_data
    eachLine = eachLine{:}
    num = eachLine(3) # this is the value I need
    numbers(index) = num
    index = index + 1
end
plot(numbers)

而已。这就是我想做的。从字符串中提取一个数字,存储在一个矩阵中,绘制它。然而,在迭代之后,numbers对象显示为整数而不是向量/矩阵!!

谁能告诉我哪里出了问题以及如何解决?

4

2 回答 2

0

尝试:

numbers = [numbers num];

代替

numbers(index) = num

于 2013-09-06T16:31:35.230 回答
0

如果我理解正确,你有一个字符串单元数组,每行有一个字符串,每行中的第三个单词是一个数字。您可能希望将代码更改为:

% example data variable
data={'air is 3 4 all';'lov3 K 9 s';'this is 7 o''clock'};

split_data = regexp(data, ' ', 'split');
len=length(split_data);
numbers=zeros(1, len);

for index=1:len
    line = split_data{index};
    num = line(3); % this is the value I need
    numbers(index) = str2double(num);
end
plot(numbers)
于 2013-09-06T18:35:35.660 回答