How do I make this into a function?

11 Ansichten (letzte 30 Tage)
JP
JP am 20 Jun. 2013
Hey, Ive been practicing making functions and it was going well until I came across this one. Im not sure if I can make it all into one function, but if possible that would be very nice! If I cant make it into one function, let me know how I can condense this a bit more. Currently it is in my main code and I would like to make my main code look nice and tidy =). Heres the code. Mostly Im shaky on what the input and output arguments should be.
figure(1);
kpressed = 0;
HF=figure(1);
set(HF,'KeyPressFcn','global kpressed; global HF; kpressed = get(HF,''CurrentChar'');');
exitflag = 0;
while (exitflag == 0)
tic
if kpressed ~= 0
switch kpressed
case ' '
exitflag = 1;
end
end
tic
fprintf(s1,'TX:0001031A')
datastr=fscanf(s1);
%example output: 0101+03088+00430-09342-01733+003378+003172-078475+019460000003100000697
data.NumberOfhandles=hex2dec(datastr(1:2));
data.HandleID=hex2dec(datastr(3:4));
%for the next function, assume the example output always gives coordinates first tool listed, then second tool
if isequal(datastr([5:11,31:37]), 'MISSINGMISSING' )
data.Q0=0;
data.Qx=0;
data.Qy=0;
data.Qz=0;
data.Tx=0;
data.Ty=0;
data.Tz=0;
data.rmsError=0;
data.PortStatus=hex2dec(datastr(12:19));
data.FrameNumber=hex2dec(datastr(20:27));
data.RotMatrix=zeros(3,3);
data.HomogeneousMatrix=zeros(4,4);
data.Euler=zeros(3,1);
data.Yaw=0;
data.Pitch=0;
data.Roll=0;
data.Q02=0;
data.Qx2=0;
data.Qy2=0;
data.Qz2=0;
data.Tx2=0;
data.Ty2=0;
data.Tz2=0;
data.rmsError2=0;
data.PortStatus2=hex2dec(datastr(12:19));
data.FrameNumber2=hex2dec(datastr(20:27));
data.RotMatrix2=zeros(3,3);
data.HomogeneousMatrix2=zeros(4,4);
data.Euler2=zeros(3,1);
data.Yaw2=0;
data.Pitch2=0;
data.Roll2=0
elseif datastr(5:11)=='MISSING'
data.Q0=0;
data.Qx=0;
data.Qy=0;
data.Qz=0;
data.Tx=0;
data.Ty=0;
data.Tz=0;
data.rmsError=0;
data.PortStatus=hex2dec(datastr(12:19));
data.FrameNumber=hex2dec(datastr(20:27));
data.RotMatrix=zeros(3,3);
data.HomogeneousMatrix=zeros(4,4);
data.Euler=zeros(3,1);
data.Yaw=0;
data.Pitch=0;
data.Roll=0;
data.Q02=str2double(datastr(31:36))/10000;
data.Qx2=str2double(datastr(37:42))/10000;
data.Qy2=str2double(datastr(43:48))/10000;
data.Qz2=str2double(datastr(49:54))/10000;
data.Tx2=str2double(datastr(55:61))/100;
data.Ty2=str2double(datastr(62:68))/100;
data.Tz2=str2double(datastr(69:75))/100;
data.rmsError2=str2double(datastr(76:81));
data.PortStatus2=hex2dec(datastr(82:89));
data.FrameNumber2=hex2dec(datastr(90:97));
data.RotMatrix2=Quat2RotMat(data.Q02,data.Qx2,data.Qy2,data.Qz2);
data.HomogeneousMatrix2=HomoMatrix(data.Q02,data.Qx2,data.Qy2,data.Qz2,data.Tx2,data.Ty2,data.Tz2);
data.Euler2=RotMat2Euler(data.RotMatrix2);
data.Yaw2=data.Euler2(1);
data.Pitch2=data.Euler2(2);
data.Roll2=data.Euler2(3);
elseif datastr(75:81)=='MISSING'
data.Q0=str2double(datastr(5:10))/10000;
data.Qx=str2double(datastr(11:16))/10000;
data.Qy=str2double(datastr(17:22))/10000;
data.Qz=str2double(datastr(23:28))/10000;
data.Tx=str2double(datastr(29:35))/100;
data.Ty=str2double(datastr(36:42))/100;
data.Tz=str2double(datastr(43:49))/100;
data.rmsError=str2double(datastr(50:55));
data.PortStatus=hex2dec(datastr(56:63));
data.FrameNumber=hex2dec(datastr(64:71));
data.RotMatrix=Quat2RotMat(data.Q0,data.Qx,data.Qy,data.Qz);
data.HomogeneousMatrix=HomoMatrix(data.Q0,data.Qx,data.Qy,data.Qz,data.Tx,data.Ty,data.Tz);
data.Euler=RotMat2Euler(data.RotMatrix);
data.Yaw=data.Euler(1);
data.Pitch=data.Euler(2);
data.Roll=data.Euler(3);
data.Q02=0;
data.Qx2=0;
data.Qy2=0;
data.Qz2=0;
data.Tx2=0;
data.Ty2=0;
data.Tz2=0;
data.rmsError2=0;
data.PortStatus2=hex2dec(datastr(82:89));
data.FrameNumber2=hex2dec(datastr(90:97));
data.RotMatrix2=zeros(3,3);
data.HomogeneousMatrix2=zeros(4,4);
data.Euler2=zeros(3,1);
data.Yaw2=0;
data.Pitch2=0;
data.Roll2=0;
else
data.Q0=str2double(datastr(5:10))/10000;
data.Qx=str2double(datastr(11:16))/10000;
data.Qy=str2double(datastr(17:22))/10000;
data.Qz=str2double(datastr(23:28))/10000;
data.Tx=str2double(datastr(29:35))/100;
data.Ty=str2double(datastr(36:42))/100;
data.Tz=str2double(datastr(43:49))/100;
data.rmsError=str2double(datastr(50:55));
data.PortStatus=hex2dec(datastr(56:63));
data.FrameNumber=hex2dec(datastr(64:71));
data.RotMatrix=Quat2RotMat(data.Q0,data.Qx,data.Qy,data.Qz);
data.HomogeneousMatrix=HomoMatrix(data.Q0,data.Qx,data.Qy,data.Qz,data.Tx,data.Ty,data.Tz);
data.Euler=RotMat2Euler(data.RotMatrix);
data.Yaw=data.Euler(1);
data.Pitch=data.Euler(2);
data.Roll=data.Euler(3);
data.Q02=str2double(datastr(75:80))/10000;
data.Qx2=str2double(datastr(81:86))/10000;
data.Qy2=str2double(datastr(87:92))/10000;
data.Qz2=str2double(datastr(93:98))/10000;
data.Tx2=str2double(datastr(99:105))/100;
data.Ty2=str2double(datastr(106:112))/100;
data.Tz2=str2double(datastr(113:119))/100;
data.rmsError2=str2double(datastr(120:125));
data.PortStatus2=hex2dec(datastr(126:133));
data.FrameNumber2=hex2dec(datastr(134:141));
data.RotMatrix2=Quat2RotMat(data.Q02,data.Qx2,data.Qy2,data.Qz2);
data.HomogeneousMatrix2=HomoMatrix(data.Q02,data.Qx2,data.Qy2,data.Qz2,data.Tx2,data.Ty2,data.Tz2);
data.Euler2=RotMat2Euler(data.RotMatrix2);
data.Yaw2=data.Euler2(1);
data.Pitch2=data.Euler2(2);
data.Roll2=data.Euler2(3);
end
figure(1)
plot3(data.Tx2,data.Ty2,data.Tz2,'b*',data.Tx,data.Ty,data.Tz,'r*')
xlim([-200 200]);
ylim([-200 200]);
zlim([-1000 -500]);
drawnow
toc
end

Antworten (1)

Jan
Jan am 3 Jul. 2013
I do not understand the question. Inserting this line on top should be enough:
function YourFunctionName
  1 Kommentar
JP
JP am 3 Jul. 2013
I wanted to be able to reference all of the variables. But I figured it out, just had to have the output argument as 'data'

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu App Building finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by