My code:
Screen('OpenWindow', 0, [0 0 0], [0 0 600 600])
Screen('FillRect', win, [0 255 0 ], [0 0 50 50]);
Screen('Flip', win);
I understand that the documented line is:
Screen('FillRect', windowPtr [,color] [,rect] )
With windowPtr as just a placeholder which needs to be replaced with a variable name to identify this particular shape. However when I'm using win to identify it, I am constantly getting the error:
Undefined function or variable 'win'.
Error in Practice_Script_1 (line 17)
Screen('FillRect', win, [0 255 0 ], [0 0 50 50]);
I don't understand what I'm doing wrong and it's probably just some noob mistake that is really frustrating me.
I don't have the Psychtoolbox, but this error message typically means that the (in this case) win variable is not defined. Have you initialized this variable prior to calling the above lines of code?
The following link creating experiments using MATLAB and Psychtoolbox has some sample code and they define the win variable as
win = Screen('OpenWindow',0, [900 900 1000], [10,10, 1100,1100]);
You will need to do something similar. Another link MATLAB cookbook does the following
% Initialize the screen with a black background
% rect is the coordinates of the screen
[win rect] = Screen('OpenWindow', 0, [0 0 0]);
ovalColor = [0 255 0]; % RGB color for the oval
rectColor = [255 0 0]; % RGB color for the rectangle
ovalRect = [100 100 300 200]; % Coordinates [x1 y1 x2 y2]
rectRect = [100 250 300 350]; % Coordinates [x1 y1 x2 y2]
Screen('FillOval', win, ovalColor, ovalRect);
Screen('FillRect', win, rectColor, rectRect);
Screen('Flip', win);
Try either option and see what happens.
Related
I"m trying to color-code each of 4 histograms using a predefined color scheme. Here's a minimal working example:
colours = [0 32 96;
192 0 0;
71 207 255;
255 143 143;
] / 255;
for i=1:4
x = randn(1,100);
subplot(1,4, i)
values = histogram(x, 'FaceColor', colours (i, :));
end
However, in the image I'm getting, the colors are actually (slightly) different, for instance for the first histogram I get (102,121,160) instead of (0,32,96):
The default histogram FaceAlpha is 0.6 (ref.). Setting it to 1 should do the trick.
histogram(x,'FaceColor',[0 1 0],'FaceAlpha',1);
Very cumbersome and unexpected.
I am plotting trajectories in Matlab using contourf. I am having an issue with the colors matching the data. I have posted my current image below. All areas that do not have data should be white as they are zero which I specifically specified in the script(they are currently blue-ish which is the the 0.1 to 1 range). In addition, values that are yellow, should be in the blue range(<1). Any suggestions?
Here is the part of my script where I do the plotting:
axesm('mercator', 'MapLatLim', latlim, 'MapLonLim', lonlim,...
'Frame', 'on', 'Grid', 'on', 'MeridianLabel', 'on', 'ParallelLabel', 'on')
setm(gca,'mlabelparallel',-20)
load coastlines
Contours = [0.001 0.01 0.1 1 10 100];
[c,h] = contourfm(latlim, lonlim, u, log(Contours));
colorbar('YTick', log(Contours), 'YTickLabel', Contours);
myColorMap = jet(256).^.3;
myColorMap(1,:) = [1];
colormap(myColorMap)
colorbar
caxis(log([Contours(1) Contours(length(Contours))]));
colorbar('FontSize', 12, 'YTick', log(Contours), 'YTickLabel', Contours);
geoshow(coastlat, coastlon,'Color', 'k')
Contour level, V, in contourfm(lat,lon,Z, V) does not scale your data or colour. It works in a different way than what you thought.
Let's see one example first:
u = rand(8)+0.1; u(1:2,:) = 0; u(5:6,:) = 10; u(7:8,:) = 100;
V = [0,1,40,100];
contourfm([0,1], [0,1], u, V);
mycm = jet(256).^.3; mycm(1,:) = 1;
colormap(mycm)
contourcbar('FontSize', 12, 'YTick', V, 'YTickLabel', V);
where u is
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0.1947 0.6616 0.2413 0.6511 0.4403 0.9112 1.0016 0.5654
0.8422 0.3159 0.5695 0.6478 0.9933 0.1686 0.8387 0.5362
10 10 10 10 10 10 10 10
10 10 10 10 10 10 10 10
100 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100
As you can see, for V = [0,1,40,100] all values from 0 to 1 are white, values from 1 to 40 are cyan and above are red.
Therefore, you must scale your u then assign appropriate contour level. Use contourcbar instead of colorbar to check the colours first.
Apart from the problem with contour level, I suspect the u parameter contains negative values. The colour at the bottom of the colour bar is always assigned to the minimum z value. You must ensure 0 is the minimum value in u, i.e. remove the negative values.
Below is some code that recreates my problem as simplified as I can make it. It does a subplot with two plots, you'll notice the plot on the right (contour only) has the correct correlation between the contour colors and the color bar but when a surface is added (left plot) the colors no longer match up.
Notes:
I've tried contourslice but I get the same results. I've posted the code for that below too.
How far off the colors are seems to depend on the values of the contour data itself. If you replace my contour data with peaks, it works fine. However this does not solve the underlying problem.
Code using contour:
clear all; close all; clc
%define box coordinates
bx = [0 1 1 0 0;0 1 1 0 0]-.5;
by = [0 0 1 1 0;0 0 1 1 0]-.5;
bz = [0 0 0 0 0;1 1 1 1 1]-.5;
%make contour data
[x,y] = meshgrid(-1:.5:1,-1:.5:1);
con = (x.^2+y.^2);
figure(1)
subplot(1,2,1)
box = surf(bx,by,bz); %draw box
set(box,'FaceColor',[1 1 1],'FaceAlpha',1,'EdgeAlpha',0,'EdgeColor',[.5 .5 .5])
hold on
camlight(30,70)
contour(x,y,con) %draw contour
colorbar
axis([-1 1 -1 1 -1 1])
axis equal
subplot(1,2,2)
contour(x,y,con)
axis([-1 1 -1 1])
axis equal
colorbar
set(gcf,'outerposition',[150 150 800 300])
Code using contourslice instead of contour (same problem)
clear all; close all; clc
%define box coordinates
bx = [0 1 1 0 0;0 1 1 0 0]-.5;
by = [0 0 1 1 0;0 0 1 1 0]-.5;
bz = [0 0 0 0 0;1 1 1 1 1]-.5;
x = -1:.5:1;
y = x;
z = x;
%make contour data
[xg,yg,zg] = ndgrid(x,y,z);
V = 3-(xg.^2+yg.^2+zg.^2);
figure(1)
subplot(1,2,1)
box = surf(bx,by,bz); %draw box
set(box,'FaceColor',[1 1 1],'FaceAlpha',1,'EdgeAlpha',0,'EdgeColor',[.5 .5 .5])
hold on
camlight(30,70)
contourslice(x,y,z,V,[],[],0) %draw contour
colorbar
axis([-1 1 -1 1 -1 1])
axis equal
subplot(1,2,2)
contour(x,y,V(:,:,3))
axis([-1 1 -1 1])
axis equal
colorbar
set(gcf,'outerposition',[150 150 800 300])
Thanks for your help!
Just set the caxis property as you wish:
colorbar
caxis([0 2])
...
colorbar
caxis([0 2])
The problem was probably caused, because the surf plot changed the color determining values of your plot. By setting a fixed color axis you can avoid all misinterpretations.
I have three different plots inside a loop: two plots have border, but one doesn't:
I want all of them to have a black border. I tried to make it by using box but the problem persists.
hold on
figure(1),plot((delta1),Sref1,'*','Color',colors(i,:));title('Frequency [500MHz-1GHz]')
gcf=figure(1);
set(gcf,'Position', [0 0 290 245]);
hold off
hold on
figure(2),plot((delta2),Sref2,'*','Color',colors(i,:));title('Frequency [1GHz-1.5GHz]')
gcf=figure(2);
set(gcf,'Position', [0 0 290 245]);
hold off
hold on
figure(3),plot((delta3),Sref3,'*','Color',colors(i,:));title('Frequency [1.5GHz-2GHz]')
gcf=figure(3);
set(gcf,'Position', [0 0 290 245]);
hold off
Just add box on before the first hold off line.
This code worked for me (Matlab 2012b):
hold on
figure(1),plot(1:10);title('Frequency [500MHz-1GHz]')
gcf=figure(1);
set(gcf,'Position', [0 0 290 245]);
box on
hold off
hold on
figure(2),plot(1:10);title('Frequency [1GHz-1.5GHz]')
gcf=figure(2);
set(gcf,'Position', [0 0 290 245]);
hold off
hold on
figure(3),plot(1:10);title('Frequency [1.5GHz-2GHz]')
gcf=figure(3);
set(gcf,'Position', [0 0 290 245]);
hold off
I'm running psychtoolbox-3.0.10 in MATLAB R2011B in a Windows XP partition in a Macbook computer. I've been trying to run a script a Matlab for behavioral sciences course (see here. The file is called FunkyScreen.m.
It works for first 30 seconds (the first 80 lines or so) and then Matlab suddenly crashes and closes. I clicked on the 'details' button of the message and it shows me that the program probably stopped because of the mex files.
Questions:
Should I manually setup Matlab to compile those files?
Or is it just crashing because of internal problems?
The script is:
% FunkyScreen.m
%
% opens a window using psychtoolbox,
% makes the window do some funky things
%
% written for Psychtoolbox 3 on the PC by IF 3/2007
screenNum=0;
flipSpd=13;% a flip every 13 frames
[wPtr,rect]=Screen('OpenWindow',screenNum);
monitorFlipInterval=Screen('GetFlipInterval', wPtr);
% 1/monitorFlipInterval is the frame rate of the monitor
black=BlackIndex(wPtr);
white=WhiteIndex(wPtr);
% blank the Screen and wait a second
Screen('FillRect',wPtr,black);
Screen(wPtr,'Flip');
HideCursor;
tic
while toc<1;
end
% make a rectangle in the middle of the screen flip colors and size
Screen('FillRect',wPtr,black);
vbl=Screen(wPtr,'Flip');% collect the time for the first flip with vbl
for i=1:10
Screen('FillRect',wPtr,[0 0 255], [100 150 200 250]);
vbl=Screen(wPtr, 'Flip', vbl+(flipSpd*monitorFlipInterval));
% flip 13 frames after vbl
Screen('FillRect',wPtr,[255 0 0], [100 150 400 450]);
vbl=Screen(wPtr, 'Flip', vbl+(flipSpd*monitorFlipInterval));
end
% blank the screen and wait a second
Screen('FillRect',wPtr,black);
vbl=Screen(wPtr,'Flip', vbl+(flipSpd*monitorFlipInterval));
tic
while toc<1;
end
% make circles flip colors & size
Screen('FillRect',wPtr,black);
vbl=Screen(wPtr,'Flip');
for i=1:10
Screen('FillOval',wPtr,[0 180 255], [ 500 500 600 600]);
vbl=Screen(wPtr, 'Flip', vbl+(flipSpd*monitorFlipInterval));
Screen('FillOval',wPtr,[0 255 0], [ 400 400 900 700]);
vbl=Screen(wPtr, 'Flip', vbl+(flipSpd*monitorFlipInterval));
end
% blank the Screen and wait a second
Screen('FillRect',wPtr,black);
vbl=Screen(wPtr,'Flip', vbl+(flipSpd*monitorFlipInterval));
tic
while toc<1;
end
% make lines that flip colors size & position
Screen('FillRect',wPtr,black);
vbl=Screen(wPtr,'Flip');
for i=1:10
Screen('DrawLine',wPtr,[0 255 255], 500, 200, 700 ,600, 5);
vbl=Screen(wPtr, 'Flip', vbl+(flipSpd*monitorFlipInterval));
Screen('DrawLine',wPtr,[255 255 0], 100, 600, 600 ,100, 5);
vbl=Screen(wPtr, 'Flip', vbl+(flipSpd*monitorFlipInterval));
end
% blank the Screen and wait a second
Screen('FillRect',wPtr,black);
vbl=Screen(wPtr,'Flip', vbl+(flipSpd*monitorFlipInterval));
tic
while toc<1;
end
% combine the stimuli
Screen('FillRect',wPtr,black);
vbl=Screen(wPtr,'Flip');
for i=1:10
Screen('FillRect',wPtr,[0 0 255], [100 150 200 250]);
Screen('DrawLine',wPtr,[0 255 255], 500, 200, 700 ,600, 5);
Screen('FillOval',wPtr,[0 180 255], [ 500 500 600 600]);
Screen('TextSize', wPtr , 150);
Screen('DrawText', wPtr, 'FUNKY!!', 200, 20, [255 50 255]);
vbl=Screen(wPtr, 'Flip', vbl+(flipSpd*monitorFlipInterval));
Screen('FillRect',wPtr,[255 0 0], [100 150 400 450]);
Screen('FillOval',wPtr,[0 255 0], [ 400 400 900 700]);
Screen('DrawLine',wPtr,[255 255 0], 100, 600, 600 ,100, 5);
vbl=Screen(wPtr, 'Flip', vbl+(flipSpd*monitorFlipInterval));
end
% blank the screen and wait a second
Screen('FillRect',wPtr,black);
vbl=Screen(wPtr,'Flip', vbl+(flipSpd*monitorFlipInterval));
tic
while toc<1;
end
Screen('CloseAll');
ShowCursor
Here is how you can (probably) eliminate the problem.
Step 1: Run the code, try to get an indication of where it crashes
Step 2: Possibly using the information from Step 1, devide your code in 2 halves.
Step 3: Run both halves separately, see which one crashes
Step 4: Keep repeating the steps above untill you have identified the culprit
Step 5: Try to deal with the specific problem that is found