ImmortalxR
ImmortalxR

Reputation: 329

Matlab string manipulation

I need help with matlab using 'strtok' to find an ID in a text file and then read in or manipulate the rest of the row that is contained where that ID is. I also need this function to find (using strtok preferably) all occurrences of that same ID and group them in some way so that I can find averages. On to the sample code:

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

Do not be mislead by the examples I posted, that first number is repeated for about 15 lines then the ID changes and that goes for an entire set of different ID's, then they are repeated as a whole group again, think [1,2,3],[1,2,3], the main difference is the values trailing the ID which I need to average out in matlab.

My current code is:

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

Thanks for any assistance.

Upvotes: 0

Views: 177

Answers (1)

Robert Seifert
Robert Seifert

Reputation: 25232

You let us guess a lot, so I guess you want something like this:

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

When you now access the index ii=6 adressing the ID = 002141865

MatrixOfCertainID6 = values{6};

you get:

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

... for further calculations.

Upvotes: 1

Related Questions