Reputation: 365
I have a table in Matlab crsp and and cell array of numbers that serve as keys. I want to retrieve information from the table using that cell array which is stored as a variable. My code is as follows:
function y = generateWeights(permno_vector, this_datenum, crsp)
crsp(crsp.PERMNO == permno_vector,:);
crsp is defined as a table while permno_vector is the cell array. It contains a couple permnos that are used to retrieve information.
In this case, my code is not working and will not allow me to access the values in crsp. How do we access table values using a vector array?
Upvotes: 0
Views: 120
Reputation: 6284
As James Johnstone points out, the first problem with the code you've posted is that it doesn't assign anything to y
, so as written your function doesn't return a value. Once you've fixed that, I assume the error you are seeing is Undefined operator '==' for input arguments of type 'cell'.
It's always helpful to include this sort of detail when asking a question.
The syntax
crsp(crsp.PERMNO == x,:)
would return those rows of crsp
that had PERMNO
equal to x
. However if you want to supply a list of possible values, and get back all the rows of your table where your target variable matches one of the values in the list, you need to use ismember
:
crsp(ismember(crsp.PERMNO, cell2mat(permno_vector)),:)
if permno_vector
is a cell array, or simply:
crsp(ismember(crsp.PERMNO, permno_vector),:)
if you can instead supply permno_vector
as a numeric vector (assuming of course the data in crsp.PERMNO
is also numeric).
Upvotes: 1