0

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?

3
  • Do you have a question? Commented Oct 9, 2017 at 22:03
  • Sorry, yes I do. The question is that it's simply not working. How can you access mutliple values in a table using such an array. Commented Oct 9, 2017 at 23:58
  • What are you expecting to output from this? Your last statement: crsp(crsp.PERMNO == permno_vector,:); isn't doing anything, i.e. you aren't assigning the output to anything or printing it. Commented Oct 10, 2017 at 7:15

1 Answer 1

1

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).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.