How to creates a value in a cell nested in a cell - matlab

This seems like a very easy question but I can't seem to work it out. Basically I have a number of cells: Face1, Face2, Scene1, Scene2. Each cell contains a value, for instance, Face1=15, Face2=23, Scene1=46, Scene2=9
Now I would like to group all these cells into one single cell called D, something like this D={Face1,Face2,Scene1,Scene2}. However, when I do this I get D={15,23,46,9}. This is not what I want. I'd like to keep the identity of each cell so when I call D{1,1} I'll get Face1, and when I call Face1, I'll get 15.
Does anyone have a suggestion?
Thanks!

Have you considered using structures? Try this:
D.face1 = 15
D.face2 = 23
You could also do it like this:
D.face(1) = 15
Let me know if that doesn't do it for you.

Probably not the best "solution", but you could do something with eval.
First, create your variables.
Face1 = 15, Face2 = 23, Scene1 = 46, Scene2 = 9
Then, create the cell array containing the variable names, as strings:
D = {'Face1', 'Face2', 'Scene1', 'Scene2'}
You can query their values like this:
>> eval(D{1,1})
Face1 =
15

Too long for a comment:
Looks like you are using arrays, not cells. That is, I think your code looks like this:
Face1 = 15
Face2 = 23
Scene1 = 46
Scene2 = 9
D = {Face1 Face2 Scene1 Scene2}
Which would act as you describe. Now D{1} returns 15.
If you were using "cells", your code would look like this:
Face1 = {15}
Face2 = {23}
Scene1 = {46}
Scene2 = {9}
D = {Face1 Face2 Scene1 Scene2}
This is probably not what you want, but it is the cell solution. Now D{1} returns {15} and D{1}{1} returns 15.
If you want to preserve the names in the composite data D, the a struct is probably the easiest thing. See Fletch's just-posted answer.

Related

Get Row and Column number from "ThisComponent.CurrentSelection" in libreoffice calc basic

I have this code where I can get which one is the current selected cell and use it to modify its value:
theSelection = ThisComponent.CurrentSelection
theSelection.setString("some value")
Now I want to move to the next column to the right, if it was Microsoft excel VBA I could just use something like theSelection.Offset(0,1) but that's not the case. So I'm doing some workarounds of course:
nextCell = oActiveSheet.getCellByPosition( ???currentColumn + 1, ???currentRow)
ThisComponent.CurrentController.select( nextCell )
I just want to know the simplest way to replace these ??? to the actual values of the theSelection var to move to the next column to the right.
I also tried this:
nextCell = oActiveSheet.getCellByPosition( column() + 1, row())
But I don't know why it is always returning column() = 1 and row() = 1 in regardless of which is the value of the CurrentSelection. Thanks in advance for the help.
Get the cell address.
Sub ChangeAndThenGoToCellToRightOfSelection
oActiveSheet = ThisComponent.getCurrentController().getActiveSheet()
oSels = ThisComponent.getCurrentSelection()
If oSels.supportsService("com.sun.star.sheet.SheetCell") Then
'A single cell is selected.
oSels.setString("some value")
address = oSels.getCellAddress()
nextCell = oActiveSheet.getCellByPosition(address.Column + 1, address.Row)
ThisComponent.CurrentController.select(nextCell)
End If
End Sub
To see what an object can do, use an introspection tool such as XrayTool or MRI.

find strings in cell array using wildcards

I have a cell array of which the last part looks like this:
Columns 8372 through 8375
{'w20091231_2000.nc'} {'w20091231_2020.nc'} {'w20091231_2040.nc'} {'w20091231_2100.nc'}
Columns 8376 through 8379
{'w20091231_2120.nc'} {'w20091231_2140.nc'} {'w20091231_2200.nc'} {'w20091231_2220.nc'}
Columns 8380 through 8383
{'w20091231_2240.nc'} {'w20091231_2300.nc'} {'w20091231_2320.nc'} {'w20091231_2340.nc'}
Columns 8384 through 8387
{'wD1.nc'} {'wD2.nc'} {'wD3.nc'} {'wD4.nc'}
Now I want to rearrange this array so that it only contains the last four strings.{'wD1.nc'} {'wD2.nc'} {'wD3.nc'} {'wD4.nc'}
I tried
IndexC = strfind(names,'wD*.nc');
Index = find(not(cellfun('isempty',IndexC)))
and
Index = find(contains(names,'wD*.nc'));
names2=names(Index)
both work if wD*.nc is wD4.nc but then of course I only select the one value and not the four that I want.
How do I get to use the * ?
I had to do some googling but found this https://www.mathworks.com/matlabcentral/answers/77039-comparing-strings-with-wildcards , and something like the following seems to work:
IndexC = regexp(names, regexptranslate('wildcard', 'wD*.nc'));
Index = find(not(cellfun('isempty',IndexC)));
names2=names(Index)
In one line using regexp with the match option:
x = regexp([x{:}],'wD\d+\.nc','match')

Swift: how to change a integer variable name within a method and reassign its value

I am a complete beginner at Swift/programming and am making a noughts and crosses app as part of an online course. I wanted to do this on my own before I saw the solution so my logic may be a bit weird.
I have a function which is called each time a button is pressed (there are 9, one for each square). The function acts to:
i) update the number of turns (which allows me to see who's go it is)
ii) change the picture to X or O
iii) deactivate the button after each turn
iv) calculate is anyone has won by changing the value of that squares potential lines. The numbers I have chosen to do this are 3 and 4 (for noughts and crosses), hence the c = 3 or 4 below. This means that if any winning line adds up to 9 or 12, the game stops as someone has won.
I have 9 variables (Int) that hold the score for each square - they are a1o, a2o, a3o, b1o... i.e all end in "o". In the function I want to add the string of "a1" in front of the "o", meaning each button is only relevant to itself with only one line of code above the function (within the button parentheses).
The function looks like this at the moment; calling it by: button(a1, buttonValue: "a1")
func button(buttonName: UIButton, buttonValue: NSString) -> String {
let a = buttonName
var b = buttonValue
b = (b as String) + "o"
print(b)
// the above prints "a1o", the name of the variable, but it is a string...
index += 1
print("Go number \(index)")
if index % 2 == 0 {
// show a nought
a.setImage(UIImage(named: "nought.png"), forState: UIControlState.Normal)
c = 3
// the above line doesn't work as its a string, but I am attempting to set the squares value to 3 (i.e. a1o = 3)
winner()
a.userInteractionEnabled = false
} else {
// show a cross
a.setImage(UIImage(named: "cross.png"), forState: UIControlState.Normal)
c = 4
// the above line doesn't work as its a string
winner()
a.userInteractionEnabled = false
}
return "done"
}
What I can't figure out is how to change the common stem of the variable *a1*o by not making it a string, or if I do, how I convert it back to a variable.
I am struggling with definitions and as a result looking for an answer has been hard. The variable a1o is an integer, but how do I refer to a1o itself?
Thank you in advance,
Sam
It is not possible to access variables dynamically by name like you want to do. However, there are better ways to accomplish the same goal. How about a two-dimensional array?
var scores: [[Int]] = [[0,0,0], [0,0,0], [0,0,0]]
scores[0][1] = 4 //this is the new version of "a2o = 4"
Unlike with variable names, the indices to the array ("0" and "1" in this case) can be dynamically chosen. Or, if you really want to keep your buttons' values exactly the same, you could use a dictionary:
var scores: [String: Int] = [:]
scores[buttonValue as String] = 4

Is there a quick way to assign unique text entries in an array a number?

In MatLab, I have several data vectors that are in text. For example:
speciesname = [species1 species2 species3];
genomelength = [8 10 5];
gonometype = [RNA DNA RNA];
I realise that to make a plot, arrays must be numerical. Is there a quick and easy way to assign unique entries in an array a number, for example so that RNA = 1 and DNA = 2? Note that some arrays might not be binary (i.e. have more than two options).
Thanks!
So there is a quick way to do it, but im not sure that your plots will be very intelligible if you use numbers instead of words.
You can make a unique array like this:
u = unique(gonometype);
and make a corresponding number array is just 1:length(u)
then when you go through your data the number of the current word will be:
find(u == current_name);
For your particular case you will need to utilize cells:
gonometype = {'RNA', 'DNA', 'RNA'};
u = unique(gonometype)
u =
'DNA' 'RNA'
current = 'RNA';
find(strcmp(u, current))
ans =
2

Extract field of struct array to new array

I have a struct, which has 2 fields: time and pose. I have multiple instances of this struct composed in an array, so an example of this is:
poses(1)
-time = 1
-pose = (doesn't Matter)
poses(2)
-time = 2
-pose = (doesn't Matter)
poses(3)
-time = 3
-pose = (doesn't Matter)
...
Now when I print this:
poses.time
I get this:
ans =
1
ans =
2
ans =
3
How can I take that output and put it into a vector?
Use brackets:
timevec=[poses.time];
tricky matlab, I know I know, you'll just have to remember this one if you're working with structs ;)
For cases that the field values are vectors (of same size) and you need the result in a matrix form:
posmat = cell2mat({poses.pose}');
That returns each pose vector in a different row of posmat.