Welcome! Log In Create A New Profile

Advanced

Dynamic Rotation of img/polygon

Posted by tonygao2004 
Dynamic Rotation of img/polygon
June 04, 2025 03:22PM
Dear Jaewon,

I am currently designing an object rotation task using MonkeyLogic. I wanted to ask if there is a way to present the object’s rotation dynamically during the sample presentation phase, rather than showing just a static rotated image. In addition, I recently experienced some difficulties of using ImageGraphic and self-uploaded images, when MonkeyLogic runs the script, the image is unable to show up and the screen suddenly freezes unexpectedly. Any guidance or suggestions you could provide would be greatly appreciated!

Thank you very much for your help!

Best,

Tony
Re: Dynamic Rotation of img/polygon
June 04, 2025 09:27PM
If you are talking about 3D rotations, the answer is no.

What do you mean by self-uploaded images? Please show your code.
Re: Dynamic Rotation of img/polygon
June 04, 2025 10:43PM
% image file
image_stim = 'stimuli1.jpg';
choices = {'CW','ACW'};
rotation_dir = choices{randi(2)}; % clockwise or anticlockwise (ACW)
if strcmp(rotation_dir,'ACW')
angle_deg = -(randi(12)-1)*30;
else
angle_deg = (randi(12)-1)*30;
end
editable('image_stim'); %% ,'rotation_dir','angle_deg'
--------------------------------------------------------

if sample %t or f
if error_type == 0 %% correct trial
response_s = run_image_scene(image_stim,angle_deg,1000,rotation_dir,Sample_hold_time); % 1000ms presentation window
if error_type == 0 && ~response_s
error_type = 3;
play_error(error_messages,error_type);
end
idle(delayedperiod)
end
end

-----------------

function snd = rotation_and_sound(rotation_dir,Sample_hold_time)
snd = AudioSound(null_);
if strcmp(rotation_dir,'CW')
snd.List = {'Correct_Response.wav',Sample_hold_time}; %% high tone for clockwise (CW) - sound check for now
else
snd.List = {'Incorrect_Response.wav',Sample_hold_time}; %% low tone for anti-clockwise (ACW)
end
end

%%% Image Rotation Scene

function response = run_image_scene(image_stimulus,angle,duration,rotation_dir,Sample_hold_time)
img = ImageGraphic(null_);
img.List = {image_stimulus,[0,0],angle,1};%% 1 - size to scale
snd = rotation_and_sound(rotation_dir,Sample_hold_time);
con = Concurrent(img);
con.add(snd); %% display auditory cues in parallel

fix = SingleTarget(tracker);
fix.Target = img;
fth = FreeThenHold(fix);
fth.WaitTime = Sample_wait_time;
fth.HoldTime = Sample_hold_time;
con.add(fth);

response = fth.Success;
scene_MR = create_scene(con);
run_scene(scene_MR,30); %% 30 - bhv code

idle(duration);
end
-------------------

This is the code that I am working on, it will not be 3D rotation but only 2D with certain degree. I have both bmp and jpg files in the same directory but it does not show up when I run this task with codes above. Also for some reason in other tasks, I have called input property 'angle' for ImageGraphic, the displayed image do not automatically rotate itself. Please let me know if there is anything that I did wrong. Thank you so much for your help!

Best,

Tony
Re: Dynamic Rotation of img/polygon
June 05, 2025 03:09PM
Sometimes an error occurs due to a simple typo. If you post only partial code, there’s not much I can tell.

One clear mistake is "response = fth.Success;", since response will always be false. If this line is for reading out the scene result, it should be put after run_scene(). Adapter chains do nothing until they are executed with run_scene().
https://monkeylogic.nimh.nih.gov/docs_CreatingTask.html#SceneAnalysis
Re: Dynamic Rotation of img/polygon
June 05, 2025 03:55PM
Thank you for the feedback, I am also wondering if there is a way to perform the rotation dynamically in monkeylogic for 2D images?
Re: Dynamic Rotation of img/polygon
June 06, 2025 10:20AM
You may think you explained something, but I have no idea what you are trying to do. You should explain what exactly you want to do, not how you want to do. Rather than "perform the rotation dynamically", for example, it is better to say like "present an image with 30-deg rotation in one scene and 60-deg rotation in the next scene" or "show a square rotating at 90 deg/s for 1 s". You seem to use the scene framework. Then, for the former, you can change the Angle property of ImageGraphic to 30 or 60 before calling run_scene(). For the latter, see GraphicProperty.
Re: Dynamic Rotation of img/polygon
June 06, 2025 07:36PM
Sorry for the confusion, I earlier meant the latter one you suggested. Thanks for the clarification. I have tried to use the GraphicProperty adapter for creating image rotation to randomized degrees in 2000ms. This is the code I currently have:

sample_img = ImageGraphic(null_);
sample_img.List = {image_stim,[0 0]};

sample_angle = get_angle();
sample_img.Scale = [sample_scale,sample_scale];
sample_img.Angle = sample_angle;

rotated_img = GraphicProperty(null_);
rotated_img.Target = sample_img;
rot_angles = linspace(0,sample_angle,n_steps);

rotated_img.Property = 'angle';
rotated_img.Value = {rot_angles};
rotated_img.DurationUnit = 'msec';

snd = rotation_and_sound(sample_angle,hold_time);
con = Concurrent(rotated_img);
con.add(snd); %% display auditory cues in parallel
tc = TimeCounter(con);
tc.Duration = hold_time;


% run the scene
scene = create_scene(tc);
run_scene(scene,30);

idle(500); % clear screen
end

However, when I tried to run this file, there is an error suggesting:

Operands to the logical AND (&&) and OR (||) operators must be convertible to logical scalar values. Use the ANY or ALL functions to reduce operands to logical scalar values.

Error in GraphicProperty/analyze (line 170)
obj.bValChanged=obj.PrevValIdx~=ValIdx&&repetition(2)<obj.Repetition;
^^^^^^^^^^^^^^
Error in Concurrent/analyze (line 7)
continue_=obj.Adapter{1}.analyze(p);
^^^^^^^^^^^^^^^^^^^^^^^^^
Error in mladapter/analyze (line 35)
continue_=obj.Adapter.analyze(p);
^^^^^^^^^^^^^^^^^^^^^^
Error in TimeCounter/analyze (line 17)
analyze@mladapter(obj,p);
^^^^^^^^^^^^^^^^^^^^^^^^
Error in rotation_runtime/run_scene (line 155)
continue_=scene.Adapter.analyze(param_);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error in rotation_runtime (line 1329)
run_scene(scene,30);
^^^^^^^^^^^^^^^^^^^
Error in run_trial (line 129)
runtime(MLConfig,TrialRecord,TaskObject,TrialData);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error in monkeylogic/UIcallback (line 894)
result = run_trial(MLConfig,datafile);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^


I have also uploaded the full script for convenience. It would be ideal if you could please help me to clarify the issue and if there is a way that I can refine my code. Thank you so much for the help!
Attachments:
open | download - rotation.m (6.2 KB)
Re: Dynamic Rotation of img/polygon
June 06, 2025 09:08PM
rot_angles should be a column vector.

The National Institute of Mental Health (NIMH) is part of the National Institutes of Health (NIH), a component of the U.S. Department of Health and Human Services.