how to access datamatrix using javascript?
Hi,
I want to copy the data in one datamatrix to another datamatrix in the same experiment.
I use python inline script and it works well:
items['new_loop_1'].dm.ID[0] = items['new_loop'].dm.ID[5]
items['new_loop_1'].dm.conditions[0] = items['new_loop'].dm.conditions[5]
Because I am going to run this experiment in OSWEB, so I need to change it to javascript.
I will be appreciated if you could help me with how I can achieve the same function using javascript.
Thank you!
Jing
Comments
Hi @jing ,
In OSWeb, the loop items do not use a DataMatrix, but a custom JavaScript object. In principle, you can access this also programmatically. Say that you want to print out the 'matrix' for an item called block_loop:
But it's not really intended to be used that way, and so it's probably best to think of a different solution.
What exactly do you want to do?
— Sebastiaan
Check out SigmundAI.eu for our OpenSesame AI assistant!
Hi @sebastiaan,
thank you very much for your reply!
In my experiment there are two loops (matrixes), the
"new_loop"is filled with data and has many columns and rows, one of the column is"ID". The "new_loop_1" is a "empty" matrix, it only has the same column names as thenew loop, but it is not filled with actual data.I want to copy special rows according to specific order from the
new_loopto thenew_loop_1, for example,the row with ID = 5 from the "new loop" will be copied to the "new_loop_1" and serves as row 0.
the row with ID = 33 from the "new loop" will be copied to the "new_loop_1" and serves as row 1.
the row with ID = 60 from the "new loop" will be copied to the "new_loop_1" and serves as row 2.
Through this way, the
new_loop_1will have the order and specfic data I want. When I run this experiment, the originally emptynew_loop_1will be filled with some specfic data fromnew loop.I do not need to print the matrix in the console, I want the data in one "matrix" be copied in another matrix according to the order I specified. I hope that I have made it clear.
Thank you again for your help!!
Best wishes,
Jing
Hello @sebastiaan,
I have solved the problem of copying special rows from one matrix to another matrix through
this.experiment.items._items["new_loop_1"].matrix[0] =this.experiment.items._items["new_loop"].matrix[5]this.experiment.items._items["new_loop_1"].matrix[1] =this.experiment.items._items["new_loop"].matrix[33]this.experiment.items._items["new_loop_1"].matrix[2] =this.experiment.items._items["new_loop"].matrix[60]Everything worked well, but there is still one problem that I was not able to solve.
That is, after copying the matrix, the
row 0in thenew_loop_1can not be presented in the sketchpad. Therow 1androw 2worked well.I have checked the console and found that:
in the first trial,
this.experiment.items._items["new_loop_1"].matrix[0].Phrase1was filled with data fromthis.experiment.items._items["new_loop"].matrix[5].Phrase1,such as "abc", butvars.Phrase1of new _loop_1was empty.In the second and third trial,
this.experiment.items._items["new_loop_1"].matrix[0].Phrase1was filled with data, such as "bcd", andvars.Phrase1was filled with the same data.This is the problem why the first trial can not be presented in the sketchpad. But I was not sure, why in the first trial the value of
this.experiment.items._items["new_loop_1"].matrix[0].Phrase1can not be automatically given to the variablevars.Phrase1, but in the following trials it worked.Could you please help me with this question?
Thank you very much!
Best wishes!
Jing
Hi Jing,
Does it work for the last trial? I don't know how the matrix object is represented internally, but it could be a problem that the first line is interpreted as header and not as content, so that it is skipped. Further alternatives could be trying to copying the information line by line.
You could also share your experiment here. Unfortunately though, without an api, I won't be able to help you much. At least, I don't know what kind of objects this matrix is and what operations are defined on it.
Hope this helps,
Eduard
Hi, @eduard,
thank you very much for your reply. Yes, it worked for the last trial, as you said, maybe the first line is interpreted as header.
I solved the problem by copying the trial to the second, third, fourth... lines, the first line is empty and unused.
this.experiment.items._items["new_loop_1"].matrix[1] =this.experiment.items._items["new_loop"].matrix[5]this.experiment.items._items["new_loop_1"].matrix[2] =this.experiment.items._items["new_loop"].matrix[33]this.experiment.items._items["new_loop_1"].matrix[3] =this.experiment.items._items["new_loop"].matrix[60]The
new_loop_1begins from line1. I leavethis.experiment.items._items["new_loop_1"].matrix[0]empty. This time it works perfect.Thank you!
Best wishes
Jing