2013-10-11 27 views
0

我需要帮助matlab使用'strtok'在文本文件中找到一个ID,然后读入或操作该ID所在行的其余部分。我也需要这个函数来查找(最好使用strtok)所有出现的相同ID并以某种方式将它们分组,以便我可以找到平均值。在示例代码:Matlab字符串操作

ID list being input: 
(This is the KOIName variable) 
010447529 
010468501 
010481335 
010529637 
010603247......etc. 

File with data format: 
(This is the StarData variable) 
ID>>>>Values 

002141865 3.867144e-03 742.000000 0.001121 16.155089 6.297494 0.001677 

002141865 5.429278e-03 1940.000000 0.000477 16.583748 11.945627 0.001622 

002141865 4.360715e-03 1897.000000 0.000667 16.863406 13.438383 0.001460 

002141865 3.972467e-03 2127.000000 0.000459 16.103060 21.966853 0.001196 

002141865 8.542932e-03 2094.000000 0.000421 17.452007 18.067214 0.002490 

不要被我张贴的例子被误导,是第一个数字是重复约15行,则ID的变化和去整组不同的ID的,那么他们是再次重复整个组,再考虑[1,2,3],[1,2,3],主要区别在于我需要在matlab中平均出来的ID的值。

我当前的代码是:

function Avg_Koi 

N = evalin('base', 'KOIName'); 

file_1 = evalin('base', 'StarData'); 

global result; 

for i=1:size(N) 
[id, values] = strtok(file_1); 
result = result(id); 
result = result(values) 
end 

end 

感谢您的任何援助。

+0

什么是“结果”?你不能用同一个词来标识一个变量和一个函数。然后你使用它作为id和值。咦? – dmm

+0

此外,假设evalin在'StarData'上工作(并且我不确定它会如何),那么file_1将不会是char变量,因此您无法在其上使用strtok。键入“whos”(不含引号)来查看您的变量。 – dmm

+0

我无法理解你的问题,但我很确定你想使用“加载”,而不是“evalin”。输入“帮助加载”。您不想评估KOIName和StarData中的表达式(因为没有),您只需要将数据加载到可以操作的Matlab变量中。 – dmm

回答

1

你让我们猜了很多,所以我想你想是这样的:

load StarData.txt 

IDs = { 010447529; 
     010468501; 
     010481335; 
     010529637; 
     010603247; 
     002141865} 

L = numel(IDs); 
values = cell(L,1); 

% Iteration through all arrays and creating an cell array with matrices for every ID 
for ii=1:L; 
    ID = IDs{ii}; 
    ID_first = find(StarData(:,1) == ID,1,'first'); 
    ID_last = find(StarData(:,1) == ID,1,'last'); 

    values{ii} = StarData(ID_first:ID_last , 2:end); 
end 

现在,当您访问索引ii=6 adressing的ID = 002141865

MatrixOfCertainID6 = values{6}; 

你:

0.0038671440 742  0.001121 16.155089 6.2974940 0.001677 
0.0054292780 1940 0.000477 16.583748 11.945627 0.001622 
0.0043607150 1897 0.000667 16.863406 13.438383 0.001460 
0.0039724670 2127 0.000459 16.103060 21.966853 0.001196 
0.0085429320 2094 0.000421 17.452007 18.067214 0.002490 

...作进一步计算。