Hi Matlab users: I am facing two strange problems. The first problem is with figure#1; it is inverted in y. The following code reads the .txt file ,which is attached, to be inverted in (y). I am wondering what could be wrong in my code that the data in the .txt file are inverted vertically. Second problem is with figure#4: Why the vertical axis is flipped in figure#4. The y axis shows the minus on top and the positive side on bottom. NOTE: this code produces the result in one minute, please run it and give me your comments.
clear all;
close all;
clc;
x1=-2.5:1e-3:2.5;
y1=-2.5:1e-3:2.5;
step=30;
randomscannb=100;
lambda0=1.31;
rf=((2.25)/2);
[X1,Y1]=meshgrid(x1,y1);
Ey2a4=exp((-X1.^2-Y1.^2)/rf^2);
Ey1a4=dlmread('C:\Users\kgazz012\Desktop\March19th\ccccc_10t_10w_1nmstep.txt');
Ey1a4c=conj(Ey1a4);
sfinalmatrix=size(x1);sfinalmatrix=sfinalmatrix(2);
figure(1);subplot(1,2,1);imagesc(real(Ey1a4));title('Ey1a4 real part')
axis square;
figure(2);subplot(1,2,1);imagesc([x1(1) x1(sfinalmatrix)],[y1(1) y1(sfinalmatrix)],real(Ey2a4));title('Ey2a4 real part');xlabel('um')
axis square;
figure(1);subplot(1,2,2);imagesc(imag(Ey1a4));title('Ey1a4 imag part')
axis square;
figure(2);subplot(1,2,2);imagesc([x1(1) x1(sfinalmatrix)],[y1(1) y1(sfinalmatrix)],imag(Ey2a4));title('Ey2a4 imag part');xlabel('um')
axis square;
fiberscan=Ey2a4+1;
selectedscan=round(rand(1,randomscannb)*(sfinalmatrix*sfinalmatrix)/(step^2));
match=0;
C=zeros(sfinalmatrix,sfinalmatrix);
Norma = sum(sum(Ey2a4.*conj(Ey2a4)))*sum(sum(Ey1a4.*conj(Ey1a4)));
c=0;
tic
for b=1:step:sfinalmatrix-300+1
for a=1:step:sfinalmatrix-248+1
c=c+1;
display(strcat('fractionexecuted=',num2str(c*(step^2)/(sfinalmatrix*sfinalmatrix))))
fib=Ey2a4(b:b+300-1,a:a+248-1);
fn=fib.*conj(Ey1a4);
C(b+150,a+124)=sum(sum(fn))./sqrt(Norma); for k=1:randomscannb
if c==selectedscan(k)
match=1;
end
end
if match==1
fiberscan(b:b+300,a:a+5)=0;
fiberscan(b:b+300,a+243:a+248)=0;
fiberscan(b:b+5,a:a+248)=0;
fiberscan(b+295:b+300,a:a+248)=0;
match=0;
end
end
end
toc
pack
result=abs(C);
figure(100);imagesc([x1(1) x1(sfinalmatrix)],[y1(1) y1(sfinalmatrix)],real(fiberscan));title('Ey2a4 with 100 random scan positions indicated');xlabel('um')
if step>1
resultx=real(result)*0;
for a=1:sfinalmatrix
for b=1:sfinalmatrix
if result(b,a)==0
elseif a>step+1 & b>step+1 & a<sfinalmatrix-step-1 & b<sfinalmatrix-step-1
hs=round(step/2);
resultx(b-hs:b+hs,a-hs:a+hs)=0*result(b-hs:b+hs,a-hs:a+hs)+result(b,a);
end
end
end
else
resultx=(result);
end
figure(3);subplot(1,2,1);imagesc([x1(1) x1(sfinalmatrix)],[y1(1) y1(sfinalmatrix)],real(resultx));title('real part of overalp');xlabel('um')
axis square;
figure(3);subplot(1,2,2);imagesc([x1(1) x1(sfinalmatrix)],[y1(1) y1(sfinalmatrix)],imag(resultx));title('imaginary part of overlap');xlabel('um')
axis square;

 Akzeptierte Antwort

Chad Greene
Chad Greene am 31 Mär. 2015

1 Stimme

Image axes are like that because image coordinates start at the top left corner if you're counting pixels. It's an old convention from image people, not from Matlab. Use
axis xy

8 Kommentare

Naema
Naema am 31 Mär. 2015
Thanks Chad. Did you run the code?. I need to know the right axes because I need to know if my result(figure#4) is shifted up or down. I do not know how to use axis xy and where exactly in the code. Please, clarify.
Chad Greene
Chad Greene am 31 Mär. 2015
I did not run the code--it points to files that are not on my computer. Place axis xy right after any imagesc calls you want to flip. You can even combine it with your axis square lines like this
axis xy square
Naema
Naema am 31 Mär. 2015
Why the field in the .txt file shows flipped vertically in figure #1. Does that means it is being used in the code while it is flipped?. Also, for figure #4, should I use the "Ticks" property to fix the flipped y-axis manually so that the minus side of "y-axis" is down and the positive side is up?
Naema
Naema am 31 Mär. 2015
thansk, will try to use axis xy square. The txt file is attached in the question , do you see it?
Chad Greene
Chad Greene am 31 Mär. 2015
I don't have your text file. Can you upload an example image?
Messing with tick marks is probably not the best choice. It's usually better to plot the right data, than to apply manual fixes after plotting.
Try this:
load mandrill
imagesc(X)
Then flip it like this:
axis xy
Flip it back:
axis ij
Or just plot it flipped from the beginning:
imagesc(flipud(X))
Naema
Naema am 1 Apr. 2015
I already used : axis xy and the axis looks ok now but also the result was flipped. SO, is the flipped result the right one? Please run the txt file and fix it your way. I got confused:(
Naema
Naema am 1 Apr. 2015
here is the .txt file attached
Naema
Naema am 2 Apr. 2015
Bearbeitet: Naema am 2 Apr. 2015
should not I only flip the y-axis but not the result? When I use axis xy, it flips the y axis and the plotted result.Please, let me now what is the correct thing to do as the plotted result is unknown to be shifted up or down.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu MATLAB finden Sie in Hilfe-Center und File Exchange

Tags

Gefragt:

am 31 Mär. 2015

Bearbeitet:

am 2 Apr. 2015

Community Treasure Hunt

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

Start Hunting!

Translated by