How to move a figure horizontally?

4 views (last 30 days)
Hannah Mathew
Hannah Mathew on 20 Feb 2016
Edited: Geoff Hayes on 22 Feb 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 Comment
Hannah Mathew
Hannah Mathew on 22 Feb 2016
Edited: Geoff Hayes on 22 Feb 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

Sign in to comment.

Answers (1)

Geoff Hayes
Geoff Hayes on 20 Feb 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 Comments
Hannah Mathew
Hannah Mathew on 22 Feb 2016
Edited: Stephen23 on 22 Feb 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 on 22 Feb 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!

Sign in to comment.

Categories

Find more on 2-D and 3-D Plots 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!