matlab if语句逻辑

时间:2014-01-28 04:30:01

标签: matlab if-statement for-loop

我必须为机器人编写代码来拾取1型或2型螺栓,然后将它们放入对应于类型1或类型2的孔中。

我遇到的问题是excel电子表格中的数据是以我无法更改其顺序的方式设置的。例如,孔类型向量将具有1 1 1 2 2 2,但螺栓类型向量将为1 2 1 2 1 2.问题是如果孔类型为1且螺栓类型为2,则程序结束并且不会将螺栓放在孔中。我需要帮助写它,所以即使螺栓类型为1且孔类型为2,它仍将它放在孔1中。

clc
clear variables
close all

Data=xlsread('Sample_Data'); %read in data from excel spread sheet
[n_rows,n_cols]=size(Data); %obtains the number of rows and columns from data

file_no=initialize('Robot_Project.txt'); 

x_hole=Data(1:6,1); % creates vector for hole x_coordinates
y_hole=Data(1:6,2); % creates vector for hole y_coordinates
bolt_type=Data(7:12,3); % creates vector seperating bolts into type 1 or 2
hole_type=Data(1:6,3); % creates vector seperating holes into type 1 or 2



for i=1:n_rows/2
    if bolt_type(i) == 1 && hole_type(i) == 1;
        pickup(file_no) % pickup function
        moveto(file_no,x_hole(i),y_hole(i)) %moves 
        putdown(file_no) % putdown function
    elseif bolt_type(i) == 2 && hole_type(i) == 1;
        pickup(file_no)
        moveto(file_no,x_hole(i),y_hole(i))
        putdown(file_no)
    end
end

1 个答案:

答案 0 :(得分:0)

for i = 1:number_of_bolts
    pickup;

    first_fitting_hole = find(hole_type == bolt_type(i),1); 
    % this line finds the first hole which matches the type of the bolt just picked up

    moveto(x_hole(first_fitting_hole),y_hole(first_fitting_hole));
    putdown;

    hole_type(first_fitting_hole)=0; 
    % set type of that particular hole to 0 (i.e. filled hole)

end
相关问题