How to move a figure horizontally?

4 visualizaciones (últimos 30 días)
Hannah Mathew
Hannah Mathew el 20 de Feb. de 2016
Editada: Geoff Hayes el 22 de Feb. de 2016
I have created gratings pattern(black and white bars), but I'm unable to move it horizontally.I have limited knowledge in matlab. Could someone help me in moving it.
  1 comentario
Hannah Mathew
Hannah Mathew el 22 de Feb. de 2016
Editada: Geoff Hayes el 22 de Feb. de 2016
I have created the gratings using these codes % Generates gratings of varying contrast
n = 101;
X = meshgrid(linspace(-pi,pi,n));
sinewave2D = 0.9*sin(5*X);
figure(1)
imagesc(sinewave2D)
axis equal;
axis off;
colormap(gray)
contrast = 1;
scaled_sinewave2D = ((contrast.*sinewave2D+1)*127.5)+1;
image(scaled_sinewave2D) % rescales numbers between -1 and 1 to lie between 1 and 256 colormap(gray(256))
axis equal;
axis off;
I need to move this figure horizontally(in random order- to right or left)I need the codes for that.Thank you

Iniciar sesión para comentar.

Respuestas (1)

Geoff Hayes
Geoff Hayes el 20 de Feb. de 2016
Hannah - how have you created the gratings pattern? As an image (2D array) or using multiple graphics objects that you have created drawn as black and white vertical bars? If the former, then you will be able to shift the image to the right (or left) by manipulating the array data. For example, suppose that you have create a 256x256 image of 16 alternating black and white (vertical) bars.
nRows = 256;
nCols = 256;
numBars = 16;
barWidth = nCols/numBars;
grtPattern = zeros(nRows,nCols);
for k=1:numBars
if mod(k,2) == 0
grtPattern(:,(k-1)*barWidth+1:k*barWidth) = 255;
end
end
h = image(grtPattern);
colormap([0 0 0 ; 1 1 1]);
set(gca,'xtick',[], 'ytick', []);
set(gca,'xticklabel',[], 'yticklabel',[]);
The above just creates the pattern. To move the pattern to the right, we remove the last column, shift the remaining columns to the right, and then insert the last column where the first one was (i.e. we wrap the columns around).
while true
% shift the image to right by one column
lastCol = grtPattern(:,end);
grtPattern(:,2:end) = grtPattern(:,1:end-1);
grtPattern(:,1) = lastCol;
set(h,'CData',grtPattern);
pause(0.5);
end
We update the CData property of the image handle h on each iteration of the loop, pausing for half a second before continuing with the next "movement".
  2 comentarios
Hannah Mathew
Hannah Mathew el 22 de Feb. de 2016
Editada: Stephen23 el 22 de Feb. de 2016
I have created the gratings using these codes
% Generates gratings of varying contrast
n = 101;
X = meshgrid(linspace(-pi,pi,n));
sinewave2D = 0.9*sin(5*X);
figure(1)
imagesc(sinewave2D)
axis equal; axis off; colormap(gray)
contrast = 1;
scaled_sinewave2D = ((contrast.*sinewave2D+1)*127.5)+1;
image(scaled_sinewave2D)
% rescales numbers between -1 and 1 to lie between 1 and 256
colormap(gray(256))
axis equal; axis off;
I need to move this figure horizontally(in random order- to right or left)I need the codes for that. Thank you
Geoff Hayes
Geoff Hayes el 22 de Feb. de 2016
Hannah - if you save the handle to your call to image as
image(scaled_sinewave2D);
then you can move your figure to the left, using the above answer as a guide:
while true
data = get(h,'CData');
% shift the image to right by one column
lastCol = data(:,end);
data(:,2:end) = data(:,1:end-1);
data(:,1) = lastCol;
set(h,'CData',data);
pause(0.5);
end
Try implementing the above and see what happens!

Iniciar sesión para comentar.

Categorías

Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by