You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Error in 'DrawTexture'
3 views (last 30 days)
Show older comments
Hi everybody,
I'm quite new to the whole matlab experience and I'm hoping someone can help me out with this.
I'm using a script to convert images to texture/mat files, that has worked before. But when put it in my main script and run it, I get the following error, anybody out there who can help =)?
Thank you so much!
Error in function DrawTexture: Missing argument
Discrepancy between a specified and supplied argument:
Specified Argument Description:
direction: Input
position: 2
presence:forbidden
formats allowed:
double
minimum M: 1
maximum M: 1
minimum N: 1
maximum N: 1
minimum P: 1
maximum P: 1
Provided Argument Description:
direction: Input
position: 2
presence: absent
Error using Screen
Usage:
Screen('DrawTexture', windowPointer, texturePointer [,sourceRect] [,destinationRect] [,rotationAngle] [, filterMode]
[, globalAlpha] [, modulateColor] [, textureShader] [, specialFlags] [, auxParameters]);
Error in encoding_2 (line 219)
Screen('DrawTexture', win, tex(trial_order(trial+1))); %uses loadJPG_as_Texture.m
13 Comments
Walter Roberson
on 6 Sep 2019
I suspect that your win variable is empty
However this is a question about details of psychtoolbox involving things not present in MATLAB itself.
Alix
on 6 Sep 2019
Hi Walter,
Thank you for your answer. True, it's about PTB but I didn't know where else to ask this...
How can my win variable be empty? The previous parts of the experiment are showing up just fine on the screen...
% Open window
[win, winRect] = Screen('OpenWindow', screen, 0);
Walter Roberson
on 6 Sep 2019
Are you sure that tex() is returning a texturepointer ? tex() normally returns 1 if it is passed a character vector that is valid latex commands, and returns [] if it is passed anything else (including a character vector that is not valid latex)
Alix
on 7 Sep 2019
the tex() refers to the function i use (see below). it works fine when i don't use the main script as function, but when i do (input arguments only subjID and runNr) it won't do it anymore...
% CONVERT IMAGES TO MAT files
theDir = [pwd filesep]; % Returns directory where the script is located
cd(theDir)
dirStim = [theDir filesep 'stim_encoding' filesep]; % folder with stimuli;
% use filesep instead of / for uniformity
files=dir([dirStim '*.jpg']); % Retrieves all JPG files in folder
for nfile = 1:length(files)
stim(nfile).fname = files(nfile).name;
img = imread([dirStim stim(nfile).fname]);
tex(nfile) = Screen('MakeTexture', win, img); %previously window instead of win
clear img
end
Alix
on 7 Sep 2019
@Guillaume: I am sorry I posted it here, the psychtoolbox forum doesn't work nearly as well and quick as it does here and I figured you guys are experts so who knows =). But if this question is too specific to PTB, please feel free to ignore/close it.
thank you though for taking a look!
Guillaume
on 7 Sep 2019
There's no problem posting your question here and I certainly did not mean to discourage you from doing so. It's just that I don't think that many of us regulars here know much about the toolbox (I've never used it). Walter may be the only one who knows something about it, so I pointed you to a forum where you may have more chance of getting help.
Walter Roberson
on 7 Sep 2019
I have not had the toolbox installed for a while (or rather I have not booted from that old disk in some time.) If you posted the source we would have more chance. At the moment my suspicion is that you have a scope problem and that tex is not what you think it is.
Alix
on 8 Sep 2019
@Guillaume: was merly ment as a thank you and sorry
@Walter, do you mean post the script? Here it goes:
This is how far I got, and like I said when I don't use it as a function, it works for some reason, but it loads in 148 instead of 147 files (but says there are 148 which is true):
function encoding_2_function (subjID, runNr)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%% GENERAL SET UP %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% General Parameters
PsychDefaultSetup(2);
KbName('UnifyKeyNames');
rng('shuffle');
Screen('Preference', 'SkipSyncTests', 1); %adapt under scanner (0 or comment out)
theDir = [pwd filesep]; % Returns directory where the script is located (pwd = directory where script is in, filesep = slash that corresponds to your computer system) % cd = theDir
dirStim = [theDir 'stim_encoding' filesep]; % folder with stimuli;
% use filesep instead of / for uniformity
% Filename of outputfile
outputfile = [theDir 'DATA' filesep 'PS_Encoding_S' num2str(subjID) '.mat'];
%% Set the expected parameters
p.stimDur = 2.5; %stimulus duration 2.5sec
p.fixDur = 0.5; % fixation cross duration 0.5sec / ISI
p.trialDur = p.stimDur + p.fixDur; %trial duration 3sec
p.stimN = 148; %total number of stimuli
p.taskDur = p.trialDur * p.stimN; %total expected duration of task (444sec = 7.4min)
p.margin = 0.003; % time(in sec) responses are not recorded between changes of stimuli, dependent on calculations between each trial, keep as low as possible)
p.thankyou = 5; % time thank you screen stays on
p.scrDist = 630; %in mm at scanner
p.scrWidth = 340; %in mm at scanner
p.prePost = 1; % in sec (10 at scanner)
p.respKey = [71 66 89 82]; % at scanner green/left, blue/right, yellow/down; keyboard g,b,y,r (r not used)
p.respInst = {'links', 'rechts', 'onder'};
%fixation cross size manually set
p.fixCrossSize = [.6 .5 .1];
%% Prepare stimulus file
% csv, comma delimited
% TrialNum, Stim, Stimtype
% Stimtype: 1 = negative, 2 = neutral, 3 = positive
[TrialNum, Stim, Stimtype] = textread('stim_encoding.csv', '%f%s%f', ...
'headerlines', 1, 'delimiter', ',');
% Create random order of trials (stims presented) (seed set at beginning; rng(shuffle))
ntrials = length(TrialNum);
trial_order = randperm(ntrials);
%% Make the output list
trialList.SubjID(:,1) = repmat(subjID, 1, p.stimN); %C1: SubjId to be repeated in 1 row for amount of p.stimN
trialList.runNr(:,2) = repmat(runNr, 1, p.stimN); % C2: run Number
trialList.trialNum(:,3) = repmat(1:p.stimN, 1); % C3: write out 1 through last trial Number (1-148)
trialList.trialOrder(:,4) = repmat(trial_order, 1)'; %C4: stim num/order of trials random
% empty counters to be filled in as we move through loop:
trialList.stimOnset(:,5) = zeros(p.stimN, 1); %C5: stim onset
trialList.stimType(:,6) = zeros(p.stimN, 1); %C6: cathegory response/Stimtype
%trialList(:,7) =
trialList.idealOnset(:,8) = p.prePost:p.trialDur:(p.stimN*p.trialDur)+(p.prePost-p.trialDur); %C8: ideal onset
%trialList(:,9)
trialList.RT(:,10) = zeros(p.stimN, 1); %C10: RT
%% Try Loop
%% Set up the Screen
HideCursor;
screen = max(Screen('Screens')); %screen = monitor name
% Set priority
Priority(MaxPriority(screen));
% Set to less verbose output
OrigScreenLevel = Screen('Preference', 'Verbosity', 1);
% Open window
[win, winRect] = Screen('OpenWindow', screen, 0);
% Get the size of the on screen window
[screenXpixels, screenYpixels] = Screen('WindowSize', win);
% Get flip interval
[IFI, nVal, flipSD] = Screen('GetFlipInterval', win, 100, 0.005, 20);
%Alpha Blending
Screen('BlendFunction', win, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
% calculate pixels per degree (ppd), uses script
PPD = visangle2stimsize(1,1,p.scrDist,p.scrWidth,winRect(3));
% Text
Screen('TextSize', win, 30);
Screen('TextFont', win, 'Helvetica');
c = 'center';
%Define CLUT colors
white = WhiteIndex(screen);
black = BlackIndex(screen);
grey = (white/2);
%% Make fixation cross
FixSize = round(p.fixCrossSize * PPD/2)*2; % Calculate the number of pixels in fractions of degree angle
for i = 1:length(p.fixCrossSize)
FixRect(:,1) = CenterRect([0 0 FixSize(i) FixSize(i)], winRect); %This makes a rectangle of the desired size (in the square brackets), positioned based on the size of winRect, which is the wise of the screen/window.
end
%FixColor = [0 0 0; 255 255 255; 0 0 0]; %Color
%ImageRect = CenterRect( [0 0 m m], winRect); %picture positioning
%% TASK CODING
% --- A1. Instructions --- %
Screen(win, 'fillrect', white);
DrawFormattedText(win, 'Taak is aan het laden', c, c, black);
Screen('Flip', win);
% --- A.2 Load in Images as mat/tex --- %
loadJPG_as_Textures; %uses script
% --- A.3 Instructions ---%
DrawFormattedText(win, 'Welkom!', c, 400, [0 153 153]);
DrawFormattedText(win, 'Je gaat een aantal afbeeldingen zien. \n\n Geef zo snel mogelijk aan of je de afbeeldingen negatief, neutraal, of positief vindt.', c, 550, [0 0 255]);
DrawFormattedText(win, 'Voor negatief druk links \n\n Voor neutraal druk onder \n\n Voor positief druk rechts', c, 800, [0 0 255]);
DrawFormattedText(win, 'Druk op een knop om te beginnen!', c, 1000, [0 153 153]);
Screen('Flip', win);
% --- A.4 Participant Ready ---%
%Press any button to begin:
key = 0;
while key == 0
[keyIsDown] = KbCheck; %keep checking if key press till:
if keyIsDown %key is pressed
key = 1;
end
end
% --- A.5 Pull the Trigger --- %
DrawFormattedText(win, 'Wachten op scanner, de taak begint zo!', c, c, black);
Screen('Flip', win);
% Press 5% to start the experiment
while KbCheck, end
while 1
[keyIsDown, ~ , keyCode] = KbCheck;
if keyCode(53); %trigger code = 5% on keyboard, or 84 (=t), test under scanner
break;
end
end
%% Set up pre fixation block and loop
escKey = 27; %esc key code
quitRun = 0;
fixDur = p.fixDur;
runStart = GetSecs; %time experiment started
Date = clock; %clock returns [year month day hour min sec] at startup of trial
Screen('FillRect', win, black);
Screen('FillOval', win, white, FixRect);
Screen('Flip', win);
while (GetSecs-runStart) <= p.prePost; %while time is less then fixation time at beginning of scan, do this
end
%% The Trial Loop - Start the experiment for real now! %%
for trial = 1: ntrials %length(trialList)
key = 0; % reset to 0 at each trial run to check for responses (change in 0 to value)
K = []; % empty array to be filled with responses later
trialStart = GetSecs; %start time of each consecutive trial
trialTime = trialStart - runStart;
trialList.stimOnset(trial,5) = trialTime; %write out trial Time of each trial run
% --- B1. Adjust Fixation Duration --- %
timeDif = abs(trialList.stimOnset(trial,5) - trialList.idealOnset(trial,8)) %absolute difference between real and ideal onset
if trialList.stimOnset(trial,5) > trialList.idealOnset(trial,8) %if real onset is later then ideal, reduse fixation duration (isi)
fixDur = p.fixDur - timeDif;
elseif trialList.stimOnset(trial,5) < trialList.idealOnset(trial,8) %if real onset earlier, add time to fixation duration/isi
fixDur = p.fixDur + timeDif;
end
% --- B2. Present the Stimuli --- %
Screen('FillRect', win, black);
if (trial ~= ntrials) %if this trial isn't last read out images
% Load the stimuli onto screen
Screen('DrawTexture', win, tex(trial_order(trial+1))); %uses loadJPG_as_Texture.m
stim_start(:,1) = GetSecs; %when each stimulus was actually presented
end
%%
save(outputfile);
Screen('CloseAll');
end
Walter Roberson
on 8 Sep 2019
theDir = [pwd filesep]; % Returns directory where the script is located (pwd = directory where script is in, filesep = slash that corresponds to your computer system) % cd = theDir
dirStim = [theDir 'stim_encoding' filesep]; % folder with stimuli;
% use filesep instead of / for uniformity
% Filename of outputfile
outputfile = [theDir 'DATA' filesep 'PS_Encoding_S' num2str(subjID) '.mat'];
It is recommended that you use fullfile() instead of constructing paths with filesep yourself.
Guillaume
on 8 Sep 2019
None of this is going to help with your error but as Walter said, using fullfile would greatly simplify the start of your code:
dirstim = fullfile(pwd, 'stim_encoding');
outputfile = fullfile(pwd, 'DATA', sprintf('PS_Encoding_S%d.mat'), subjID);
There's also plenty wrong with this block:
%% Make the output list
trialList.SubjID(:,1) = repmat(subjID, 1, p.stimN); %C1: SubjId to be repeated in 1 row for amount of p.stimN
trialList.runNr(:,2) = repmat(runNr, 1, p.stimN); % C2: run Number
trialList.trialNum(:,3) = repmat(1:p.stimN, 1); % C3: write out 1 through last trial Number (1-148)
trialList.trialOrder(:,4) = repmat(trial_order, 1)'; %C4: stim num/order of trials random
% empty counters to be filled in as we move through loop:
trialList.stimOnset(:,5) = zeros(p.stimN, 1); %C5: stim onset
trialList.stimType(:,6) = zeros(p.stimN, 1); %C6: cathegory response/Stimtype
%trialList(:,7) =
trialList.idealOnset(:,8) = p.prePost:p.trialDur:(p.stimN*p.trialDur)+(p.prePost-p.trialDur); %C8: ideal onset
%trialList(:,9)
trialList.RT(:,10) = zeros(p.stimN, 1); %C10: RT
The first line assign to the first column of subjID, the 2nd line to the 2nd column of a completely different variable runNr. The first column of runNr will be filled with 0s and never be used. And so on, RT will have 10 columns, the first 9 filled with 0s and never used.
The repmat are also a bit confusing, the ones that do something create row vectors which are then assigned to column vectors, a bit sloppy, and others are just repmat(x, 1) which are pointless, you just get x. This would be a lot better:
trialList.SubjID = repmat(subjID, p.stimN, 1); %C1: SubjId to be repeated in 1 row for amount of p.stimN
trialList.runNr = repmat(runNr, p.stimN, 1); % C2: run Number
trialList.trialNum = (1:p.stimN)'; % C3: write out 1 through last trial Number (1-148)
trialList.trialOrder = trial_order(:); %C4: stim num/order of trials random
% empty counters to be filled in as we move through loop:
trialList.stimOnset = zeros(p.stimN, 1); %C5: stim onset
trialList.stimType = zeros(p.stimN, 1); %C6: cathegory response/Stimtype
trialList.idealOnset = (0:p.stimN-1)' * p.trialDur + p.prePost; %A simpler way to create your vector
trialList.RT = zeros(p.stimN, 1); %C10: RT
and of course, later when you use the variables, you don't bother with the column index, eg.
timeDif = abs(trialList.stimOnset(trial) - trialList.idealOnset(trial)) %absolute difference between real and ideal onset
Accepted Answer
Alix
on 10 Sep 2019
Yes, sorry! It was so simple in the end:
1) I made sure that only jpg's are in the stimulus folder (as the transformation function posted above counts all files, which gives an error later in the script when it seems as if there are more files to be read in then there actually are)
2) when writing your script as a function 'tex' is not a texturepointer but refers to something else (as @Walter suggested in the beginning). So, when converting jpg to texture, instead of saying
tex(nfile) = Screen('MakeTexture', win, img);'
I changed it to
imgtex(nfile) = Screen('MakeTexture', win, img);'.
In the main script, it now reads
Screen('DrawTexture', win, imgtex(trialList.stimNr(trial)));
btw. tested it under the scanner (fMRI) yesterday and it works like a charm =)
Thank you guys again!
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 (한국어)