You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How to present randomly 70 different images on 70 different trials from a set of 161 images which is stored in a particular folder ?
1 view (last 30 days)
Show older comments
ANKIT MAURYA
on 14 Jan 2023
I have a folder which consists of 161 images. I want to run total 70 trials and want to present different images on different trials randomly out of these 161 pictures. I have written a code, but not able to understand what is the problem. Kindly help me. I don't know much about programming
The code I wrote is ----
folder = 'F:\Ankit \Bisection_Task \Manipulable_objects'
totalimg = dir(fullfile(folder, '/*.bmp'));
as = numel(totalimg);
imageseq = randperm(as);
for i=1:70
idx = imageseq(i);
img = imread(totalimg(idx).name);
end
Accepted Answer
dpb
on 14 Jan 2023
Edited: dpb
on 14 Jan 2023
Your code works except you're not doing anything inside the loop with each image in turn; you're just reading in and overwriting the img variable every pass so when the loop ends you always have whichever was the last image data in the permuted list.
What is your intent here; show each image to a user and interact with it or do some other processing on each image in turn? Whatever it is, you need to do something with each image before going on to the next INSIDE the loop.
One refinement could be something like
Nselect=70; % use variable, don't bury "magic numbers" in code
imageseq=randperm(numel(totalimg),Nselect); % generate the permutation vector
for i=1:Nselsect
img=imread(totalimg(imageseq(i)).name);
% now do your thing with this image here, then go on to the next...
...
end
25 Comments
ANKIT MAURYA
on 15 Jan 2023
Hi dpb,
Thanks a lot for this. I am able to run the code now perfectly.
I have just another followup query. I am able to present randomly one different image on 70 different trials out of 161 pictures stored in a folder named Manipulable_objects. I want to check which of the one image out of 161 images is presented on each of these 70 trials. It's like, I want to know what/which image is presented on trial by trial basis.
I am just posting chunks of my code here----------
I would appreciate your help and thanks again for resolving my previous query
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%TEST EXPERIMENT PHASE BEGINS%%%%%%%%%%%%%%%%%%%%%%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% TRIAL STRUCTURE %%%%%%%%%%%%%%%%%%%%%%%%%%%%
start = GetSecs;
for i_trial = 1:70
% Draw the fixation cross in white, set it to the center of our screen and % set good quality antialiasing
Screen('DrawLines', window, allCoords,...
lineWidthPix, white, [xCenter yCenter], 2);
% Flip to the screen
Screen('Flip', window);
% Wait for 500 ms
WaitSecs (0.5);
%%%%%%%%%%%%% Selecting manipulable image randomly from
%%%%%%%%%%%%% Manipulable_objects folder %%%%%%%%%%%%%%%%%%%%5
folder ='F:\Ankit\2nd Study_Jan_2023\Manipulable_objects'
manipulable_image = dir(fullfile(folder, '/*.bmp'))
as = numel(manipulable_image);
imageseq = randperm (as);
for i=1:as
idx =imageseq(i);
img = imread(fullfile(folder,manipulable_image(idx).name));
end
%%%%%%%%%%%%% Another way of Selecting manipulable image randomly from
%%%%%%%%%%%%% Manipulable_objects folder %%%%%%%%%%%%%%%%%%%%
% folder ='F:\Ankit\2nd Study_Jan_2023\Manipulable_objects'
% fileList = dir(fullfile(folder, '/*.bmp'));
% sortOrder =randperm(length(fileList));
% fileList =fileList(sortOrder);
% for k = 1: length(fileList)
% baseFileName = fileList(k).name;
% fullFileName = fullfile(folder,baseFileName);
% img = imread(fullFileName);
% end
imageTexture =Screen('MakeTexture',window,img); % Convert image into texture
% Draw the rect to the screen
Screen('DrawTexture', window, imageTexture,[],[], 0);
% Flip to the screen
Screen('Flip', window);
WaitSecs (Delay_time (i_trial)); % random durations for which images to be presented on screen
DrawFormattedText(window, 'Closer to Shorter or Longer duration?', 'center', 'center', white); % Question asked to Participants
Screen('Flip', window)
%%%%%%%% Accepting mouse responses from Participants %%%%%%%%%%%%%%
%%%%%% SHORT = 1 [left mouse button]; LONG = 2 [right mouse button]%%%%%%%%%
while 1
[x1,y1,buttons,~,~,~] = GetMouse();
if buttons(1)== 1
Mouse_Button_Pressed(i_trial)= 1 ;
break;
end
if buttons(3) == 1
Mouse_Button_Pressed(i_trial)= 2 ;
break;
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Creating data matrix%%%%%%%%%%%%%%%%%%%%%%
data = zeros (70,5); % data is a variable that will take up the value of Delay_time and Key_Pressed
data(:,1)= sub_no;
data(:,2)= Trial_Number;
data(:,3)= Delay_time*1000; % ':' is for the row and 1 is the 1st column
data(:,4)= Mouse_Button_Pressed; % ':' is for the row and 2 is the 2nd column
data(:,5)= Condition;
currentFolder = pwd;
% pwd- present working directory
dbase = strcat(currentFolder,'\Subject_', sub_name,'_',num2str(sub_no),'_Mains_Exp_Hand','.xls');
% dbase is a variable where particular file with particular name and number is saved
a = xlswrite((dbase), data); % Write the data into 'csv' file
T = array2table(data,'VariableNames',{'Subject No.','Trial_No.','Stim_dur(ms)','Response_Button','Condition'}); % converts the 'data' named matrix into table form and then add titles/headlines to each of the variables
writetable (T,(dbase)) % writes that table into output file
% Clear the screen
sca;
dpb
on 15 Jan 2023
"which of the one image out of 161 images is presented on each of these 70 trials. ..."
You have that information in two forms -- the number of the ith value of the permuation on each pass is
imageseq(i) for each I in the loop while the actual file associated with that indes is totalimg(imageseq(i).name)
ANKIT MAURYA
on 15 Jan 2023
Edited: ANKIT MAURYA
on 15 Jan 2023
What i understand from the code is -----
idx =imageseq(i);
idx stores the random ith value of imageseq (which stores 161 random values/images). Let's say while executing the code, last image (on 70th trial) presented is named 'XYZ', so idx returns the ith value of only 'XYZ' named image and not the other trials ith value. What addition/changes i need to make in the above code (long one sent by me) so that I can get ith value for every 70 trials ??
I am only able to get the last value of idx after I run the code successfully. I want the value of idx on each of the 70 trials so that my outful file contains 70 idx values for 70 trials. I hope i am able to present my question before you clearly. If not, please ask and i will clarify it further. Thank You
dpb
on 15 Jan 2023
Same thing as before -- do what you want with the specific element of the permutation vector array inside the loop with the loop index, i, while at each iteration.
It's identically the same issue as with the image variable itself, if you store into the same variable each pass through the loop, it overwrites the previous value and so if you wait until the loop is done, then all you have in that variable is the last one. Ergo, you have to consume the value of the variable inside the loop each pass through the loop, not wait until the loop is done.
ANKIT MAURYA
on 15 Jan 2023
I am sorry dpb, i am new to the programming and don't know much. I am unable to understand what you are saying. Can you help me in just writing that particular line of code, so that i can access values for each trial?
kindly help me. Thank You
dpb
on 15 Jan 2023
I just did above -- you already have it in idx =imageseq(i); is the permutation vector value, the point is to use it at that point before you write over it again in the loop next iteration.
The actual file name is the same thing -- you are addressing it to open the file; use the same expression to access/display/store the file name each pass through the loop instead of waiting until the loop finishes and you're only left with the final iteration.
Set a breakpoint and use the debugger to stop at that point in the loop and look at what the variables are at that point each iteration...
ANKIT MAURYA
on 15 Jan 2023
"the point is to use it at that point before you write over it again in the loop next iteration"
How to extract values of idx at that point only before overwriting it?
dpb
on 15 Jan 2023
You already have it -- use it THEN. What do you want to use it for? Whatever that use is, put that code inline inside the loop so it executes every pass through the loop.
ANKIT MAURYA
on 15 Jan 2023
Yes i have the value of idx, but only for the last one. I want the value of idx for each of the 70 different trials. The idx value will tell me which image was presented on respective 70 trials. Th only problem is that in the workspace, idx variable is giving only one value. And i want 70 values of idx variable.
"Whatever that use is, put that code inline inside the loop so it executes every pass through the loop"
I already put 'idx' inside the 'for' loop only. Below is the chunk of code ----
folder ='F:\Ankit\2nd Study_Jan_2023\Manipulable_objects'
manipulable_image = dir(fullfile(folder, '/*.bmp'))
as = numel(manipulable_image);
imageseq = randperm (as);
for i=1:as
idx =imageseq(i);
img = imread(fullfile(folder,manipulable_image(idx).name));
end
dpb
on 15 Jan 2023
Edited: dpb
on 15 Jan 2023
Yes, you have it as I've said repeatedly. The point is now USE it for whatever purposes you wish; it is being used to index into the directory structure array to return the file; there's nothing keeping you from using it for any other purpose you wish other than you just don't seem to want to do so...
disp(idx)
would throw it out to the command window; you can do anything you like with it; you just need to to decide what that "to do" is and put the code for it right there, inside the loop.,
ANKIT MAURYA
on 15 Jan 2023
Yes, you have it as I've said repeatedly.----------------I am also saying that i have it but only the last value, not all the 70 values. I hope you are getting my point. I want all the 70 values of idx not only one value
ANKIT MAURYA
on 15 Jan 2023
what can be done to store all the 70 values for 70 different trials in idx
dpb
on 15 Jan 2023
Why would you do that? You've already got all 70 values in the array that defined them...use it for that purpose. All idx is is a convenient name for the one you're using at a given time...
dpb
on 15 Jan 2023
"imageseq stores 161 random values"
Then you broke my code -- imageseq should be only the number you've requested...
Nselect=70; % use variable, don't bury "magic numbers" in code
imageseq=randperm(numel(totalimg),Nselect); % generate the permutation vector
...
ANKIT MAURYA
on 15 Jan 2023
Kindly have a look at my code (which is a long one i sent) please. I have not used your code later. PLEASE go through my code once from the begining and you will get to know where is the problem.
ANKIT MAURYA
on 15 Jan 2023
This one ------------------------------
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%TEST EXPERIMENT PHASE BEGINS%%%%%%%%%%%%%%%%%%%%%%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% TRIAL STRUCTURE %%%%%%%%%%%%%%%%%%%%%%%%%%%%
start = GetSecs;
for i_trial = 1:70
% Draw the fixation cross in white, set it to the center of our screen and % set good quality antialiasing
Screen('DrawLines', window, allCoords,...
lineWidthPix, white, [xCenter yCenter], 2);
% Flip to the screen
Screen('Flip', window);
% Wait for 500 ms
WaitSecs (0.5);
%%%%%%%%%%%%% Selecting manipulable image randomly from
%%%%%%%%%%%%% Manipulable_objects folder %%%%%%%%%%%%%%%%%%%%5
folder ='F:\Ankit\2nd Study_Jan_2023\Manipulable_objects'
manipulable_image = dir(fullfile(folder, '/*.bmp'))
as = numel(manipulable_image);
imageseq = randperm (as);
for i=1:as
idx =imageseq(i);
img = imread(fullfile(folder,manipulable_image(idx).name));
end
%%%%%%%%%%%%% Another way of Selecting manipulable image randomly from
%%%%%%%%%%%%% Manipulable_objects folder %%%%%%%%%%%%%%%%%%%%
% folder ='F:\Ankit\2nd Study_Jan_2023\Manipulable_objects'
% fileList = dir(fullfile(folder, '/*.bmp'));
% sortOrder =randperm(length(fileList));
% fileList =fileList(sortOrder);
% for k = 1: length(fileList)
% baseFileName = fileList(k).name;
% fullFileName = fullfile(folder,baseFileName);
% img = imread(fullFileName);
% end
imageTexture =Screen('MakeTexture',window,img); % Convert image into texture
% Draw the rect to the screen
Screen('DrawTexture', window, imageTexture,[],[], 0);
% Flip to the screen
Screen('Flip', window);
WaitSecs (Delay_time (i_trial)); % random durations for which images to be presented on screen
DrawFormattedText(window, 'Closer to Shorter or Longer duration?', 'center', 'center', white); % Question asked to Participants
Screen('Flip', window)
%%%%%%%% Accepting mouse responses from Participants %%%%%%%%%%%%%%
%%%%%% SHORT = 1 [left mouse button]; LONG = 2 [right mouse button]%%%%%%%%%
while 1
[x1,y1,buttons,~,~,~] = GetMouse();
if buttons(1)== 1
Mouse_Button_Pressed(i_trial)= 1 ;
break;
end
if buttons(3) == 1
Mouse_Button_Pressed(i_trial)= 2 ;
break;
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Creating data matrix%%%%%%%%%%%%%%%%%%%%%%
data = zeros (70,5); % data is a variable that will take up the value of Delay_time and Key_Pressed
data(:,1)= sub_no;
data(:,2)= Trial_Number;
data(:,3)= Delay_time*1000; % ':' is for the row and 1 is the 1st column
data(:,4)= Mouse_Button_Pressed; % ':' is for the row and 2 is the 2nd column
data(:,5)= Condition;
currentFolder = pwd;
% pwd- present working directory
dbase = strcat(currentFolder,'\Subject_', sub_name,'_',num2str(sub_no),'_Mains_Exp_Hand','.xls');
% dbase is a variable where particular file with particular name and number is saved
a = xlswrite((dbase), data); % Write the data into 'csv' file
T = array2table(data,'VariableNames',{'Subject No.','Trial_No.','Stim_dur(ms)','Response_Button','Condition'}); % converts the 'data' named matrix into table form and then add titles/headlines to each of the variables
writetable (T,(dbase)) % writes that table into output file
% Clear the screen
sca;
ANKIT MAURYA
on 15 Jan 2023
Thanks a lot dpb. I will try to run your code and if i have any problem , will contact you.
I really appreciate your time and effort for such a long discussion.
Let me try this code by tomorrow (In India, it is midnight now, so i left the lab now). I will tell you the reason why i was not able to run your code. But now i will try again and will let you know if there is any further problem.
But thanks a lot again. Your effort and time is highly worthy for me.
ANKIT MAURYA
on 15 Jan 2023
Can you explain me this line ? ---
imageseq=randperm(numel(totalimg),Nselect);
ANKIT MAURYA
on 16 Jan 2023
Hi dpb,
I tried your code but it's repeating the same image again and again on every trial.
KKindly please look the whole code that i sent to you and tell me where is the problem.
Thank You
dpb
on 16 Jan 2023
Edited: dpb
on 16 Jan 2023
You persist in not heeding advice...there's no point in my continuing to tell you the same thing over and over if you're not going to take anything I say to heart.
Since you were producing the randomized images before, then you've broken something else, not that part....and you don't have my code in there, anyway...so, look for something else that you've changed.
There's no difference in the use of
imagseq=randperm(as);
and
imagseq=randperm(as,Nselect);
excepting that the latter will have only the number of elements in it to solve your problem of wanting only the 70 elements after the loop is done instead of all 161 from the unconstrained permutation vector. Again, read the docs.
>> randperm(6) % unconstrained permutation vector
ans =
2 4 3 6 5 1
>> randperm(6,4) % select only four out of the six instead...
ans =
4 6 5 2
>>
While it's not as clean a solution, you could simply use
imagseq=randperm(as); % return the whole permutation vector
imageseq=imagseq(1:Nselect); % keep the number wanted
and be at the same place.
Again, can't urge strongly enough to quit burying magic numbers like the 70 in the code; use variables so they can be changed in one place and use the debugger to find where you've made logic errors...
Good luck...
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)