I'm programming a task for a research study. The problem I have is the following: I have a dictionary with names and with one picture per cell. Like that:
nameCues1 = {'Lisa','Anna', 'Sarah', 'Nina', 'resized_1.jpg' };
nameCues2 = {'Emma', 'Lena', 'Gabi', 'Steffi', 'resized_2.jpg' };
I have loaded them into a cell array, created a random sequence:
nameCuesAll = {nameCues1,nameCues2};
randSeq3 = nameCuesAll(randperm(size(nameCuesAll,2)));
Then I loop over it to read in the names of the dictionary and the corresponding picture:
for i = 1:numel(nameCuesAll)
pics3{i} = imread(randSeq3{i}{1,5});
ind{i}=randSeq3{i}(randperm(numel(randSeq3{i})));
end
Then I prompt it on the screen via Psychtoolbox, a toolbox specialized in creating tasks for research, for those who don't know:
for j = 1:4
% (left out unnecessary other functions)
DrawFormattedText(window,ind{i}(j), 'center', 'center', white, [], [], [], [], [], rect);
end
The problem is that the names of the dictionary are not shown in randomized order, and every try I had until now has thrown errors.
The main problem is that I don't know how to correctly randomize/index
the dictionary-names.
Thanks for any ideas!
To reorder the elements of a cell array, you should reference elements using ():
nameCues1 = {'Lisa','Anna', 'Sarah', 'Nina', 'resized_1.jpg' };
rIdx = randperm(numel(nameCues1));
mixedCues = nameCues1(rIdx)
which yields for the case of rIdx = [3 5 1 4 2]:
mixedCues =
1×5 cell array
{'Sarah'} {'resized_1.jpg'} {'Lisa'} {'Nina'} {'Anna'}
Then use mixedCues instead of nameCues1.
See also: Access Data in Cell Array.
Related
I saved the information of this table in an 2D array using Google Apps Script. The whole array building process worked as aspected.
Area
X
Y
Major
Minor
Angle
SensorNo
1
25049
380.500
246.500
190.953
167.023
0
1
2
24248
393.500
247.000
192.976
159.986
0
2
3
18250
382.500
247.000
159.023
146.121
0
3
However, as I tried to build a for loop (as seen in the following code) that goes through every column of the first row of the array I did not get the expected output. I tried to tell the loop that when the array content equals "SensorNo" to save the respective n as a new variable and then break the loop. But whenever the array content I'm looking for is in the last column the variable remains undefined...
for(n=0;n<fileContentArray[0].length;n++){
if(fileContentArray[0][n]=="SensorNo"){
var sensorNoCol=n;
break;
}
}
I hope someone can help me.
Best, Max
function getHeaderColumns() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Sheet Name");
const hRow = 1;
const hA = sh.getRange(hRow, 1, 1, sh.getLastColumn()).getValues()[0];
let col = {};
hA.forEach((h, i) => { col[h] = i + 1 });
return col['SensorNo'];
}
I have a set of documents in Firestore in this format. Questions array will 10 questions.
I want to get the data of questions field: one row for one question
I do I code in the appscript to perform this
This is my code so far (for one document only)
function test(){
const firestore = getFirestore();
var query = firestore.getDocument("QuestionCollection/test").fields;
var data = {};
data.subject = query.subject;
data.questions= query.questions;
const sheet = SpreadsheetApp.getActiveSheet();
for(i = 0 ; i < 10 (no. of question); i++){
const row = [data.questions[i].answer, data.questions[i].difficulty];
sheet.appendRow(row);
}
}
Error:
TypeError: Cannot read property 'answer' of undefined
Modification points:
When I saw your sample data in the image, it seems that the array length of questions is 2. But at the for loop, the end index of loop is 9. I think that by them, such error occurs.
When you want to put the value of "Serial", it is required to add the value for putting to Spreadsheet.
In your script, appendRow is used in a loop. In this case, the process cost becomes high.
When above points are reflected to your script, it becomes as follows.
Modified script:
Your for loop is modified as follows.
From:
for(i = 0 ; i < 10 (no. of question); i++){
const row = [data.questions[i].answer, data.questions[i].difficulty];
sheet.appendRow(row);
}
To:
var values = [];
for (var i = 0; i < data.questions.length; i++) {
const row = [i + 1, data.questions[i].answer, data.questions[i].difficulty];
values.push(row);
}
sheet.getRange(sheet.getLastRow() + 1, 1, values.length, values[0].length).setValues(values);
For the end index of loop, the array length is used.
Reference:
setValues(values)
You shouldn't query the .fields property directly (because your data isn't converted properly). Assuming you're using v28+ of the library, your code should look something like this:
function test(){
const firestore = getFirestore();
const query = firestore.getDocument("QuestionCollection/test").obj; // Don't use .fields here
const sheet = SpreadsheetApp.getActiveSheet();
const values = [["Serial", "Answer", "Difficulty"]]; // Init 2D array with Header row
// Loop through each question in the array and extract necessary values to construct Data rows
for (const question of query.questions){
values.push([query.questions.indexOf(question) + 1, question.answer, question.difficulty]);
}
// Replace 1, 1 below with coords of "Serial" header cell
const range = sheet.getRange(1, 1, values.length, values[0].length);
range.setValues(values);
// sheet.getRange(subjRow, subjCol).setValue(query.subject); // Add location for Subject data
}
I saw that you wanted "Serial" to represent "Question number", so I added that column to the header and data rows.
As Tanaike mentioned, there's a huge performance hit for writing to the spreadsheet in a loop, so it's better if you set up a 2D array of values to write all at once using range.setValues(array2D). Ideally, you'll want to minimize the calls to the Spreadsheet API.
Disclaimer: I'm an active contributor to the FirestoreGoogleAppsScript library.
1.I have this array and i want to extract only the "rate" values.
I want y Array to look like this
array = [6,355.1675 , 4,826.3112 , 5,429.8488]
[{
code = USD;
description = "United States Dollar";
rate = "6,355.1675";
"rate_float" = "6355.1675";
symbol = "$";
}, {
code = GBP;
description = "British Pound Sterling";
rate = "4,826.3112";
"rate_float" = "4826.3112";
symbol = "£";
}, {
code = EUR;
description = Euro;
rate = "5,429.8488";
"rate_float" = "5429.8488";
symbol = "€";
}]
It looks like you have JSON that contains an array of dictionaries.
Write some code that:
Decodes the JSON into native objects. At the simplest it would be converted to an array of dictionaries.
You could then write code that loops through the array, extracts the rate value for each array entry, and prints it.
As others have said, you could do this with a map statement, but if you're new to programming then the for loop approach is easier to understand.
If you array is called array, then you can do it this way:
array.map { print ($0["rate"] }
I have a cell array, like so:
ID = {'g283', 'sah378', '2938349dgdgf', 'g283'};
I also have some data that corresponds to these IDs.
Data = {'data1', 'data2', 'data3', 'data4'};
Let's say my current ID is g283, and I want to extract Data that matches this ID.
I do strfind(ID, 'g283') and get a result like so:
result = {[1], [], [], [1]}
I now want to extract the data from data and get this:
new_data = ['data1', 'datat4'] or equivalent.
However, cell arrays cannot be subindexed into, so I am wondering if there is an easy method to do this without looping. Thank you!
Let the input variables be defined as
ID = {'g283', 'sah378', '2938349dgdgf', 'g283'}; % ID
Data = {'data1', 'data2', 'data3', 'data4'}; % data
s = 'g283'; % current ID
You only need to apply isempty to test if each result of strfind contains a match or not. This can be done via cellfun, for example as follows:
ind = cellfun(#(x) ~isempty(strfind(x, s)), ID);
new_data = Data(ind);
If you are looking for the whole string (as opposed to a partial match), a simpler alternative is to use ismember:
ind = ismember(ID, s);
new_data = Data(ind);
I have a SKShapenode creating a line that detects collisions but only for the first line. I can continue drawing lines after but only the first line has physics properties. How do I make an array of shape nodes that all will detect collisions? Drawing is working and didBeginContact method is working fine for first collision so I think the problem is in applying the physics category to an array and not just the first one drawn.
let line = SKShapeNode()
line.path = path
line.strokeColor = UIColor.blackColor()
line.lineWidth = 5
[self.addChild(line)]
lineArray.append(line)
line.physicsBody = SKPhysicsBody(edgeChainFromPath: path)
line.physicsBody?.dynamic = false
line.physicsBody?.categoryBitMask = PhysicsCategory.Lines
line.physicsBody?.contactTestBitMask = PhysicsCategory.Ball
line.physicsBody?.collisionBitMask = PhysicsCategory.Ball
line.physicsBody?.usesPreciseCollisionDetection = true