Matlab plots black border - matlab

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

Related

Scatter plot in Matlab with x-axis ticks not equidistant

Consider the following scatter plot in Matlab
A=[1 0 0 0 2 0 0 0 0 0 0 0 1 2 0 0 0 2 1 200 300]';
xRange = 0: max(A);
prob=zeros(size(xRange,2),1);
for r=1:size(xRange,2)
prob(r) = sum(ismember(A,xRange(r)))/size(A,1);
end
scatter(xRange,prob, 'b');
xlim([-2 max(A)+2])
ylim([-0.5 1.5])
I want to change the way the scatter looks like in order to make it more clear: the idea is to put the following ticks on the x-axis
[-0.5 0 1 2 3 301]
but the tricky part is that they should be equidistant so that I can zoom on the part of the scatter plot with higher values of prob.
Any idea?
One way to achieve this is to transform the data to a new scale using interpolation. Let's say you want the data values
tickVal = [-0.5 0 1 2 3 301];
to appear at the graphical positions
tickPos = 0 : 5;
Determine the graphical positions for all the data by interpolation, and plot the transformed data:
xTransformed = interp1(tickVal, tickPos, xRange);
scatter(xTransformed, prob, 'b');
xlim([min(tickPos), max(tickPos)])
ylim([-0.5 1.5])
Now we have to make sure that the ticks do not reflect the transformed, but the original data values:
set(gca, 'XTick', tickPos)
set(gca, 'XTickLabel', num2cell(tickVal))
The result looks like this:
Is this what you want?

Contour colors don't correspond to color bar when a surf plot is added

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.

FillRect Psychtoolbox constantly causing error

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.

Modifying subplot axes in matlab

I'm plotting 2 subplots, and I want each subplot to have different axis scaling.
My current code is:
subplot(2,1,1)
axis([0 20 0 1])
plot(t,Ca,'-.',t,Cb,'.',t,Cc);
subplot(2,1,2)
axis([0 5 0 1]);
plot(t2,Ca2,'-.',t2,Cb2,'.',t2,Cc2);
But both subplots plot as if no axis was specified.
Any ideas?
Try:
subplot(2,1,1)
plot(t,Ca,'-.',t,Cb,'.',t,Cc);
axis([0 20 0 1])
subplot(2,1,2)
plot(t2,Ca2,'-.',t2,Cb2,'.',t2,Cc2);
axis([0 5 0 1]);
plot will refigure the axes, so you have to call axis AFTER the last plot.

How can I display a 2D binary matrix as a black & white plot?

I have a 2D binary matrix that I want to display as a black and white plot. For example, let's say I have a 4-by-4 matrix as follows:
1 1 0 1
0 0 1 0
1 1 0 1
1 0 0 0
How can this be plotted as a black and white matrix? Some of my input binary matrices are of size 100-by-9, so I would ideally need a solution that generalizes to different sized matrices.
If you want to make a crossword-type plot as shown here (with grid lines and black and white squares) you can use the imagesc function, a gray colormap, and modify the axes properties like so:
mat = [1 1 0 1; 0 0 1 0; 1 1 0 1; 1 0 0 0]; % Your sample matrix
[r, c] = size(mat); % Get the matrix size
imagesc((1:c)+0.5, (1:r)+0.5, mat); % Plot the image
colormap(gray); % Use a gray colormap
axis equal % Make axes grid sizes equal
set(gca, 'XTick', 1:(c+1), 'YTick', 1:(r+1), ... % Change some axes properties
'XLim', [1 c+1], 'YLim', [1 r+1], ...
'GridLineStyle', '-', 'XGrid', 'on', 'YGrid', 'on');
And here's the image you should get:
I'm not sure if I got your question right, but you may try the image function, like this:
A = [ 1 1 0; 1 0 1; 1 1 1 ];
colormap([0 0 0; 1 1 1 ]);
image(A .* 255);
Try the spy function to start with perhaps.