trying to convert IDL codes to MATLAB

2 次查看(过去 30 天)
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:
print,'PLEASE SELECT YOUR REGION OF INTEREST: 1=box, 2= enter values'
read,roichoice
case roichoice of
1: begin
box_cursor,x,y,nx,ny
print,"Margins and dimensions:",x,y,nx,ny
end
2: begin
print, 'x?'
read,x
;print, 'y?'
;read,y
print, 'nx?'
read,nx
;print,'ny?'
;read,ny
end
else: goto, roichox
endcase
my translation so far is
roichoice=input('PLEASE SELECT YOUR REGION OF INTEREST: 1=box, 2= enter values\n');
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
  2 个评论
John D'Errico
John D'Errico 2017-8-8
You used goto? No goto in MATLAB. Try again.
dpb
dpb 2017-8-9
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
Kris Fedorenko 2017-8-11
编辑:Kris Fedorenko 2017-8-11
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:
roichoice=input('PLEASE SELECT YOUR REGION OF INTEREST: 1=box, 2= enter values\n');
while ~((roichoice == 1) || (roichoice == 2))
disp('You must enter either 1 or 2')
roichoice=input('PLEASE SELECT YOUR REGION OF INTEREST: 1=box, 2= enter values\n');
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 :)
  2 个评论
hasan alhussaini
hasan alhussaini 2017-8-25
Hi Kris, i was wondering how would you go about changing the code from imcorp to imrect or rbbox?
Kris Fedorenko
Kris Fedorenko 2017-8-25
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by