Reputation: 21
I am stuck with a problem in a project I am doing and was hoping Matlab could save me countless hours.
I have an array containing strings with a corresponding integer in the column next to it (pipe IDs and lengths). I have around 7000 pipe IDs with a corresponding length.
If I have the IDs for say 200 pipes as strings in an array, is it then possible to locate these 200 individual strings within the large array and have MATLAB give me the corresponding pipe length (integer) located in the cell next to the ID string?
I have tried with ismember
and strmatch
but I cannot get it working.
EDIT:
The large array containing all the data looks like:
No. ID Length (m)
1 0-T103-0-T110 52.327
2 0-T104-0-1370 30.4
3 0-T104-0-1410 62.423
4 0-T105-0-T109 46.611
...
7118 0415B-99878 152.242
I would then have a smaller array in the same form as above, but instead of 7118 rows I would have for instance 200.
What I have tried so far:
a = {'0-T103-0-T110', 52.327; '0-T104-0-1370', 30.4; '0-T104-0-1410', 62.423};
ismember(a(:,1), '0-T104-0-1370');
leng = a{ismember(a(:, 1), '0-T104-0-1370'), 2}
This works, but as you can see it is only for locating a single string within a small array.
I have loaded the large arrays as this:
[num, text, raw] = xlsread('Pipelength_ALL.xlsx', 'A1:C7115');
Upvotes: 2
Views: 337
Reputation: 124563
You want to use ISMEMBER. Here is an example:
%# list of all pipe ID's and their length (100 in this example)
pipes = [cellstr(num2str((1:100)','pip%03d')) num2cell(randi(50,[100 1]))];
%# pipeID's of the query (10 here)
x = randperm(100);
p = cellstr(num2str(x(1:10)','pip%03d'));
%# find matching lengths
[~,loc] = ismember(p, pipes(:,1));
len = cell2mat(pipes(loc,2));
A sample of the result:
>> [p num2cell(len)]
ans =
'pip096' [14]
'pip043' [ 9]
'pip095' [27]
'pip074' [ 6]
'pip001' [ 3]
'pip065' [20]
'pip067' [ 8]
'pip060' [23]
'pip051' [27]
'pip020' [31]
Using the data you posted, the code would be:
pipes = {
'0-T103-0-T110' 52.327
'0-T104-0-1370' 30.4
'0-T104-0-1410' 62.423
'0-T105-0-T109' 46.611
}
p = {
'0-T104-0-1370'
'0-T105-0-T109'
}
[~,loc] = ismember(p, pipes(:,1));
[p pipes(loc,2)]
Upvotes: 3