Add a new records to a cell array matlab - arrays

I am using the following function in matlab:
getgenpept(AccessionNumber)
where the only parameter is a unique identifier. The problem is that I want to have a structure with around 50 different records all based on their unique identifier. Is there a way I can define a structure, and then add in my 50 different records as I go along, or ideally specify a list of identifiers before hand and loop the getgenpept() function in one go?
I want to end up with a cell array that stores each record in their own cell.
Hope this is clear!

If A is a cell array containing all the identifiers, then it's as easy as:
A = {'AAA59174', 'AAA59175','AAA59176'};
B = cellfun(#getgenpept,A);
B(1) is then the record for 'AAA59174', and so on.

Related

How can I write an array formula that extracts unique values from a list only if they have a certain value in the column next to them?

I have an understanding of how index/match can be used to create a unique list of items. I'm trying to create a unique list of items in a specific category and that is presenting a challenge to me.
For instance, say I have:
INDEX SPY
INDEX SPX
GOLD GLD
GOLD JNUG
GOLD GLD
INDEX SPY
and I want to extract only the unique GOLD values from the list. The output should be: GLD, JNUG.
I wrote a separate array formula to check if the left column is GOLD then output the symbol from the right column but the problem I'm having is that whatever I set for false ends up looking like it's part of the list to the index/match formula.
I was thinking that maybe there's a way to remove all zero-value array elements before exposing it to index/match but I'm out of my league here. Any suggestions on how to get the job done efficiently would be appreciated. Thank you!
Let's assume your data is in A1:B6. With UNIQUE and FILTER this is pretty easy now (Excel 2021+). Use the following in one cell, and it will spill:
=UNIQUE(FILTER($B$1:$B$6,$A$1:$A$6="GOLD"))
In earlier versions, it's a bit more work. You can use the following array formula (Ctrl+Shift+Enter to enter). Here entered into cell D3:
=IFERROR(INDEX($B$1:$B$6,MATCH(0,IF("GOLD"=$A$1:$A$6,COUNTIF($D$2:D2,$B$1:$B$6),""),0)),"")
You drag this formula down until you reach empty cells (the IFERROR() wrapper converts the #N/As that appear after all unique values have been found into ""). The "magic" is here done by IF("GOLD"=$A$1:$A$6,COUNTIF($D$2:D2,$B$1:$B$6), supplying INDEX() with new rows only for those rows that contain "GOLD" in $A$1:$A$6.

Excel array formula - managing data in table with dynamic height

I have a working array formula, which creates a list of values in column A. The length of the list depends on the input values of the formula and will vary over time. Values could be removed and added anywhere in the list, not necessarily at the end. Now in column B I want to manually add comments on the values in column A. When the length of the list changes, the values in column B don't move along with the values in column A, so they are not longer on the correct line. Is there a way to solve this?
You can try to use the OFFSET function for the dynamic variable.

Retrieving data from table using cell array - Matlab

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

Excel - pass an array into a function

On one sheet in my file I have a number of arrays defined as named ranges. One another sheet I'd like to use a drop down, or something similar to select the name of one of the named range and have the data/contents of that named range populate a range on the second sheet. Is this possible WITHOUT VBA? Is there an array formula that will do this?
One, not entirely elegant, method that I've thought of is using the index function and copy this within a range of cells equivalent in size to the size of the largest named range. Something like:
=INDEX(range_1,ROW(),COLUMN())
This requires me to be able to pass the name of a named range into a function though. Is this possible?
Answers to either or both of these questions would be greatly appreciated.
Without that, the only other way I can think to do this is using a brute force, offset look up, which I'd prefer not to do.
Thanks for your help.
Indirect might do what you want it to.
In Sheet1 I created 3 named ranges:
Then in Sheet2, I
1) Put these names in Column A
2) Used a data validation list linked to column A to place a name in a cell (C2)
3) Used the array formula {=INDIRECT(C2)} (accepted with Ctrl+Shift+Enter) in the cells that I wanted to hold the array (C4:E5)
When C2 is changed via the drop-down, the array is automatically changed:

How to Create a table in C that consists of Arrays of fixed lenght

I am trying to implement a search table that basically consists of arrays of a fixed size. In my case each array would consist of 4 elements (Say for ex letters W,X, Y and Z). I need each element in the array to have a fixed index in the table using which it can be found and accessed by the user. The table would something like this..( the symbol | is used below to show the ending of that particular array)
WXYZ|XYWZ|WXZY|....|.... and so on
Could somebody tell me which is the best way to implement htis? I have heard of linked lists and hash tables but I am not sure if that is the best method to do this..
I can't get it, why don't you just use an ordinary two dimensional array like array[100][4], the user can access through an ordered pair of two indexes.

Resources