# trying to convert IDL codes to MATLAB

16 views (last 30 days)
hasan alhussaini on 8 Aug 2017
Commented: Kris Fedorenko on 25 Aug 2017
Hi i'm trying to create a similiar code on matlab to the one i have in IDL
Basically the code lets you select a box_cursor over a region of an image or select the x,y,nx,ny deminsions
the IDL codes are
roichox:
case roichoice of
1: begin
box_cursor,x,y,nx,ny
print,"Margins and dimensions:",x,y,nx,ny
end
2: begin
print, 'x?'
;print, 'y?'
print, 'nx?'
;print,'ny?'
end
else: goto, roichox
endcase
my translation so far is
switch( roichoice)
case 1
[I ,rect]=imcrop();
yb=rect(1);
xb=rect(2);
nyb=rect(3);
nxb=rect(4);
fprintf('Margins and dimensions: x=%f y=%f nx=%f ny=%f',xb,yb,nxb,nyb);
case 2
xb=input('x?');
nxb=input( 'nx?');
otherwise
goto(roichox);
return
end

John D'Errico on 8 Aug 2017
You used goto? No goto in MATLAB. Try again.
dpb on 9 Aug 2017
As John says, there is no GOTO in Matlab; leave the otherwise clause empty and enclose the block in a while loop. Use break when get a valid response to exit the block and go on to next step.

Kris Fedorenko on 11 Aug 2017
Edited: Kris Fedorenko on 11 Aug 2017
As dpb said above, you can use a while loop instead of "goto", which does not exist in MATLAB. Here is one possible example of how you can make it work:
while ~((roichoice == 1) || (roichoice == 2))
disp('You must enter either 1 or 2')
end
switch( roichoice)
case 1
[I ,rect]=imcrop();
yb=rect(1);
xb=rect(2);
nyb=rect(3);
nxb=rect(4);
fprintf('Margins and dimensions: x=%f y=%f nx=%f ny=%f',xb,yb,nxb,nyb);
case 2
xb=input('x?');
nxb=input( 'nx?');
end
Hope this helps! And YAY for switching from IDL :)

hasan alhussaini on 25 Aug 2017
Hi Kris, i was wondering how would you go about changing the code from imcorp to imrect or rbbox?
Kris Fedorenko on 25 Aug 2017
Hi Hasan,
The difference would be in what these functions return:
• imrect returns a handle to an imrect object. Based on the documentation, you can get the x,y position and dimensions by using a "getPosition" function. For example:
rectHandle=imrect();
rect = getPosition(rectHandle);
where rect(1) is x position, rect(2) is y position, rect(3) is width, and rect(4) is height.
• rbbox returns a four-element vector, [x y width height], where x and y are the x and y components of the lower left corner of the box, and width and height are the dimensions of the box. But rbbox returns immediately if a button is not currently pressed. You can use rbbox with waitforbuttonpress, so that rbbox returns when you release the mouse button:
waitforbuttonpress
rect = rbbox();
Another difference is that rbboc would define the box in the coordinates of the figure rather than in the axes coordinates.
Hope this helps!
Kris