I wrote several cell arrays I read from txt-files in cell arrays within the MATLAB Workspace. I wrote a cell array 'Names', containing the names of the other cell arrays I assigned. The txt-files can have varying names, therfore I want to use this dynamic naming.
Now I would like to the cell arrays dynamically by using the name (string) from 'Names'. Is this possible? Which syntax do I have to use?
Logically Names{i} gives back the name of the cell array in position i. However using Names{i}.n does not work.
I could not find anything in the MATLAB documentation either..
Related
I am currently working on machine learning, and as so I have an array in which the first column is the data and the second column is the label. Data was originally a cell array from Matlab (Not sure if that is important).
[My Array of arrays] https://i.stack.imgur.com/JFpWO.png
To make sure that everything is as it should be I would like to extract one of the arrays in index 0 and check its dimensions with the numpy.shape function. Currently, if I try that I just get the shape of the bigger array IE. (394,2)
Any ideas?
There are different ways.: Try..
arr[:,1,:]
or
arr[:,1]
or
[a[1] for a in arr]
Let's say I have in B2 the formula =ROW(A1:A3). This formula gives an array of {1;2;3}. Because a cell only holds one value, cell B2 displays a value of 1. Any place in the worksheet, the formula =B2 gives 1, not the array. Yet, Excel still remembers the array because the formula is still in B2.
Is there any way to get the array back so it, the whole array, not its individual elements, can be used for further manipulation? I'm looking for something like OPERATION(B2) = {1;2;3}. For example, SUMPRODUCT(OPERATION(B2)) = SUMPRODUCT(ROW(A1:A3)) = 6.
As a workaround, you can store your formula in Name Manager, e.g.:
Then you can use it as a reference in Excel formulas, like =INDEX(Rows,2,1):
I realize that this is not the answer to the OP's question as they do not have the latest Office 365. I put this here for those who come later and do have it.
With the latest Office 365 with Dynamic array formulas this problem is now gone.
Putting =ROW(1:3) or the equivalent dynamic array formula =SEQUENCE(3) Excel will spill the array down automatically without the need of Ctrl-Shift-Enter:
And now one can refer to the array by only refering to A1 by putting # after the reference:
=SUM(A1#)
Then no matter how the array in A1 changes the other formula does not need to be changed.
The caveat is that the formula in A1 needs the room to spill down. If there is a cell that has anything in it that resides in the spill down array it will return an error.
Just to try and clarify this, arrays in Excel before the September 2018 update for Office 365 don't self-expand like they do in Google sheets, so if you put an array into a single cell you just get the first element of the array (see column A for example).
If you want to enter the array directly into the sheet, you have to select the cells you want it to occupy (say B2:B4) and enter it as an array formula (see column B) using CtrlShiftEnter
You can still use the array in another formula as long as it's expecting an array - e.g. with sumproduct you get the correct total, 6 (see column C).
=SUMPRODUCT(ROW(A1:A3))
Unfortunately this doesn't always work without entering the whole formula as an array formula (see column D).
=SUM(ROW(A1:A3))
EDIT
#JvDV is correct, you don't always get the first element of an array expression entered into a single cell - see this reference for explanation of the implicit intersection mechanism.
I have two methods, which might qualify as solutions or workarounds, depending on what technology you're willing to accept.
Both rely on retrieving the formula that produces the array from B2 and then evaluating that formula each time you invoke your OPERATION(B2).
Method 1: VBA
Create a UDF:
Function Arr(R as Range)
Arr = Application.Evaluate(R.Formula)
End Function
which you then invoke in a cell, e.g. =SUM(Arr(B2)).
Method 2: Name Manager + EVALUATE()
Note that this is different from Justyna's answer. The idea is to allow you to specify the address of the cell holding the array-generating formula.
Create a name that holds =EVALUATE(FORMULATEXT(INDIRECT(INDEX($1:$1048576;ROW();COLUMN()+1)))). This way you can specify that you want the content of the cell B2 by writing the string B2 to the nearest cell to the right of the cell in which you're using your newly created name.
Unfortunately, EVALUATE() cannot be used within a cell.
How does one generate an array whose elements are cell values or math operation of cell values? For example, how does one generate an array of {0,1,A1,B1}) or {8,3,A1/B1,A1*B1} or {0,1,A1/B1-2, A1*B1+3}?
I have not found anything on the net regarding this topic.
I am new to matlab. I have one question about datatypes.
I have an array of the cell array type. In the variables window it shows me it as 190x1 double.
I have two questions.
What is the purpose of the cell array type if there is the matrix type ?
How to append another similar array to the column of an existing array. For example I have two arrays 190x1 double I wan't a new array to be 380x1 double
I would be grateful for any help.
Thanks
I'd encourage you to browse the documentation for cell arrays. One of the most common uses as it states is mixed size or mixed type data...among other uses.
Cell arrays commonly contain either lists of text strings,
combinations of text and numbers, or numeric arrays of different
sizes.
To append two arrays you could do this. It will place the two vectors end to end.
X=ones(190,1);
X =[X; X];
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: