How to pull an image by its name

6 views (last 30 days)
M-JB
M-JB on 11 Oct 2017
Edited: M-JB on 11 Oct 2017
Hi all. I'm very new to the MATLAB, and I wonder if you could help me with my issue.
There are 2 folders. folder 1 - 100 images - 3 conditions folder 2 - 100 images - 3 conditions
Images are named from 1 to 100 with additional letters depending on the condition, for example 1up, 2up, 3in, 4bu, 5up, 6in etc.
Loop goes simply from 1-100 and then moves to the next folder and displays another 100 images.
So I wonder, how to call the image not by the number but by the condition? (by the text that is next to the number).
Here is the code I have:
expinfo.nBlock = 2; % number of blocks
expinfo.nrPic = 100; % number of trials per block
for blockNumber = 1:expinfo.nBlock
dirName = ['./Stimuli/Block',num2str(blockNumber),'/'];
file_list = dir([dirName,'*.jpg']);
im = cell(length(file_list),1);
for file = 1:length(file_list)
im{file} = imread([dirName,file_list(file).name]);
end
for j = 1:length(im)
im_tex(j) = Screen('MakeTexture', winPtr, im{j});
end
wf = 0; % resets the counter for pictures
response = -1;
while response == -1
wf = wf +1;
if wf > expinfo.nrPic
response = 1;
if response == 1
break;
end
end
end
What I am trying essentially do is to call markers by using the condition name. For example, before I have used the key button to do so:
if keyIsDown
if keyCode(space) && ~keyCode(rightKey) && ~keyCode(leftKey)
mrk2 = wf+100;
outlet.push_sample(mrk2);
disp(['Now sending: ' int2str(mrk2)])
end
end
However, in this script there is no response from participants so I have to associate the markers with something else, thus I thought I could use the condition name that is next to the number. If I could achieve that I could get a fully working script without disturbing the loop. Any thoughts? Many thanks!

Answers (1)

Cam Salzberger
Cam Salzberger on 11 Oct 2017
Edited: Cam Salzberger on 11 Oct 2017
Once you have the file name, you could use some parsing to remove the number and extension and just get the condition text. Then you can use that text for whatever you want (it's not clear to me what you mean by "call"). Here's an example using regular expressions:
fName = '1up.jpg';
% Ignore digits at the start, accept anything but a period as a condition, and match any extension
fCond = regexp(fName, '\d+([^\.])+\.', 'tokens');
fCond = fCond{1}{1};
If you want to do something with key presses, you'd probably have better luck adding a KeyPressFcn or WindowKeyPressFcn callback to the figure, and react there. But it can only handle single key strokes, not entering a name.
If you want users to enter a full name and do something then, probably better off giving them an edit uicontrol to type in, and create a callback for that.
-Cam

Categories

Find more on Scripts in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!