Reputation: 5646
I'm working in MATLAB and I have the following cell array:
pippo =
'FSize' [ 10]
'MSize' [ 10]
'rho' [ 997]
'u2' [ 86.2262]
'n' [ 100]
'nimp' [ 2]
'impeller1dir' [1x66 char]
'impeller2dir' [1x66 char]
'comparedir' [1x57 char]
I would like to return the content of the cell, in the second column, which corresponds to a given value for the cell in the first column of the first row. I.e., if the input is 'nimp', I want to return 2. Is there a simple way to do this which doesn't involve looping, or is looping the only way?
Upvotes: 0
Views: 83
Reputation: 3587
Two methods to do this are containers.Map
and logical indexing
firstly we will find the occurance of the input in the first column with strcmp
using ind=strcmp(pippo(:,1),'nimp')
and then get the contents of the cell in the second column where this is true pippo{ind,2}
which can be combined into one line with
out = pippo{strcmp(pippo(:,1),'nimp'),2}
using containers.Map
you can map the keys in the first column to the values in the second column this information is stored as a container, below this is the pippo2 variable
pippo2=containers.Map(pippo(:,1),pippo(:,2))
and then you can call the container with an argument of the key and get the value as output
out=pippo2('nimp')
out =
2
Upvotes: 1