How to pass selected Gtk TreeView row to Gtk TreeStore insert function? - gtk3

I am trying to insert a row into a Gtk.TreeStore, for which I have to pass the selected row number from Gtk.TreeView. I found the solution for PyGTK but not for PyGObject.
For PyGTK the insert function looks like this (http://www.pygtk.org/pygtk2reference/class-gtktreestore.html#method-gtktreestore--insert):
def insert(parent, position, row=None)
The position can be queried like this:
treeview = Gtk.TreeView()
selection = treeview.get_selection()
model, iter = selection.get_selected()
path = iter.get_selected_rows()[0]
index = path.get_indices()[0]
But in PyGObject I get the error:
self.index = self.path.get_indices()[0]
AttributeError: 'LayerDataStore' object has no attribute 'get_indices'
How can I get the integer value of the row number? Am I approaching the problem in a weird way? It seems like the solution should be simpler and have less code.
Further Reading:
This is the description of the insert function in GTK3:
https://lazka.github.io/pgi-docs/Gtk-3.0/classes/TreeStore.html#Gtk.TreeStore.insert
Similar question for PyGTK:
Getting the row number of Gtk TreeView
Similar question for C++:
Get data from selected row of gtk treeview - gtkmm, c++

Finally figured it out. But I am not sure if there is a simpler solution:
First call select on the TreeView:
tree = Gtk.TreeView()
select = tree.get_selection()
select is then a Gtk.TreeSelection. We use this object to call get_selected_rows():
selected_rows = select.get_selected_rows()
The function returns a tuple of a LayerDataStore and a GtkTreePath when a row is selected. Otherwise the GtkTreePath is an empty list [].
Then assign the GtkTreePath to a variable:
path = selected_rows[1]
Path is now a list of the GtkTreePath or an empty list [] if nothing is selected. You can insert an if-function here to avoid getting any Errors.
We then have to unpack the list by using:
row = path[0]
Now the variable row is a TreePath and the print function will return 0 for the first row, 1 for the second row and so on. For nested trees it will return 0:0 for the first nested object in the first row, 0:1 for the second object in the first row and so on.
With the get_indices function we can convert the TreePath to a list:
index = row.get_indices()
The print function will now print [0] for the first row and [1] for the second. The nested objects are [0,0] for the first object of the first row and [0,1] for the second nested object of the first row.
Because I am just interested in the row number itself I am using this assignment to get only the row number:
row_number = index[0]
Finally the row number is passed to the TreeStore:
store.insert(None, row_number, [True, "New Layer")])
Helpful links:
https://lazka.github.io/pgi-docs/Gtk-3.0/classes/TreeSelection.html
https://lazka.github.io/pgi-docs/Gtk-3.0/structs/TreePath.html
https://lazka.github.io/pgi-docs/Gtk-3.0/classes/TreeView.html
https://lazka.github.io/pgi-docs/Gtk-3.0/classes/TreeStore.html

Related

append element to array in for loop matlab

I have a matrix 10x500 and I want to discard every row which contains in the first 100 elements a value above 6. First I am trying to make an array with all the indexes of the row to discard. Here my code
idx_discard_trials = [];
for i = 1:size(data_matrix,1)
if any(data_matrix(i,1:100)>6)
idx_discard_trials = i;
end
end
However, at the end of the loop I get just the last index, not a list. Does anybody know how to append elements to an array using a for loop?
It's because you keep rewriting a single value, you need to append the values through idx_discard_trials(end+1) = i, for example.
You don't need a loop for this however, try the following:
data_matrix(any(data_matrix(:,1:100) > 6, 2),:) = []

document.querySelectorAll get innerText of ALL selected elements at once pure javascript

I want to get all innerText of a whole column of a very long html table (random length).
I'm using this code:
var tbEls = document.querySelectorAll('#tBodyID tr td:nth-child(cidx)');
Where cidx = the column index I want to extract content from.
But such code extracts all the td elements (with the innerText inside them of course).
But it doesn't extract directly all the innerText inside them. Cause of this I have to reprocess the returned tdEls array with a for loop to extract from each tbEls[i] element its own innerText. It works but...
My question is:
In pure JS (no external libraries or frameworks) is it possible to use a more direct approach improving some way just and only the querySelectorAll parameter ('#tBodyID tr td:nth-child(cidx)') to get directly all the td elements innerText at once and in just one javascript statement and without the need of reprocessing the returned array with the for loop or anything else?
In other words is there a some kind of innerText selector that can be used to get them all at once without any kind of extra loop?
No problem at all if it is not recognized by old browsers, I'm sorry for them.
What I hope to achieve is something like:
var arrTblColInnerText = document.querySelectorAll('#tBodyID tr td:nth-child(cidx):alltd:innerText');
I want to get an array similar to:
0: value from column cidx cell 0
1: value from column cidx cell 1
2: value from column cidx cell 2
3: value from column cidx cell 3
...
n: value from column cidx cell n
Thanks in advance.
The easiest way I found was to convert the nodeList to an array first then use a map:
var nodes = document.querySelectorAll("h3 em");
var list = [].slice.call(nodes);
var innertext = list.map(function(e) { return e.innerText; }).join("\n");
Here's a one-liner from 2021. It says to take the NodeList returned from the querySelectorAll and make it an Array, then map the innerText into an array.
Array.from(document.querySelectorAll("h3 em")).map(x => x.innerText)

Postgres get entire array from multidimensional array

I'd like to do the following:
CREATE TABLE example ( name text, arr text[][]);
INSERT INTO example VALUES ( 'aa', '{}');
UPDATE example SET arr = arr || ARRAY['header'] WHERE name = 'aa';
Right here, I have arr = [['header']]. I can easily add another array making it arr = [['header'], ['another']] by calling the last line from above again with 'another' inside the array constructor. However, I'm looking to add elements to those inner arrays now. Something like...
UPDATE example SET version[1] = version[1] || ARRAY['more'] WHERE name = 'aa'
However, postgres throws the error, wrong number of subscripts. I understand that postgres multidimensional arrays must have the same dimensions inside, so I would have to be able to add an element to all the inner arrays at once (probably NULL) and then change the one I'm adding to. Is there any way to do this without some kind of loop?

How do I iterate through an imported excel doc?

what I need to do for this code is import a giant (302x11) excel doc, and then ask the user for an input. Then, I need to iterate through each element in the 5th column of the excel array, and if that element matches the user input, save the entire row to a new array. After going through all 302 rows, I need to display the new array.
So far, I have this:
Vin = input('Vin: ');
filename='MagneticCore.xlsx';
sheet=2;
xlRange='B2:L305';
[ndata, text, alldata] = xlsread(filename,sheet,xlRange,'basic');
After this, I'm not sure how to iterate through the alldata array.
alldata is a cell, to select the fifth column you can use alldata{:,5}. Searching in Cells is done this way without iterating
Try it on your own, if you get stuck update your question with code and error message
Daniel R is right, you can do this without iterating through the cell array. Here's how you could iterate through the array if you needed to:
[ndata, text, alldata] = xlsread('Book1.xlsx');
target = 12;
newArray = {};
for r = 1:size(alldata, 1)
% get the element in the fifth column of the current row
e = raw{r,5};
if e == target
% add to newArray
newArray{end + 1} = alldata(r,:);
end
end
% display newArray
for r = 1:size(newArray, 1)
disp(newArray{r})
end

If a MATLAB function returns a variable number of values, how can I get all of them as a cell array?

I am writing a function to remove some values from a cell array, like so:
function left = remove(cells, item);
left = cells{cellfun(#(i) ~isequal(item, i), cells)};
But when I run this, left has only the first value, as the call to cells{} with a logical array returns all of the matching cells as separate values. How do I group these separate return values into a single cell array?
Also, perhaps there is already a way to remove a given item from a cell array? I could not find it in the documentation.
You have to use () instead of {} to index the cells:
function left = remove(cells, item)
left = cells(cellfun(#(i) ~isequal(item, i), cells));
Using () for indexing will give you a subset of cells, while using {} will return the contents of a subset of cells as a comma-separated list, and only the first entry of that list will get placed in left in your example.
You can check out this MATLAB documentation for more information on using cell arrays.
EDIT: Response to comment...
If you have an operation that ends up giving you a comma-separated list, you can place the individual elements of the list into cells of a cell array by surrounding the operation with curly braces. For your example, you could do:
left = {cells{cellfun(#(i) ~isequal(item, i), cells)}};
The inner set of curly braces creates a comma-separated list of the contents of cells that are not equal to item, and the outer set then collects this list into a cell array. This will, of course, give the same result as just using parentheses for the indexing, which is the more sensible approach in this case.
If you have a function that returns multiple output arguments, and you want to collect these multiple values into a cell array, then it's a bit more complicated. You first have to decide how many output arguments you will get, or you can use the function NARGOUT to get all possible outputs:
nOut = 3; %# Get the first three output arguments
%# Or...
nOut = nargout(#some_fcn); %# Get all the output arguments from some_fcn
Then you can collect the outputs into a 1-by-nOut cell array outArgs by doing the following:
[outArgs{1:nOut}] = some_fcn(...);
It should be noted that NARGOUT will return a negative value if the function has a variable number of output arguments, so you will have to choose the value for nOut yourself in such a case.