【问题标题】:Matlab if statement logicMatlab if 语句逻辑
【发布时间】:2014-01-28 04:30:01
【问题描述】:

我必须为机器人编写代码以拾取类型 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

【问题讨论】:

  • 当然这只是您的if 声明的另一个扩展,就像您的elseif bolt_type(i) == 2 && hole_type(i) == 1 一样?也就是bolt 1有type 2,bolt 2有type 2的情况加行?

标签: matlab if-statement for-loop


【解决方案1】:
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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多