Matlab如何识别字符字母都有的内容(比如X88)并加工

我的第一个任务是将一个Fusion360中创建的图形文件导出的G代码自动转换为Matlab可用并可继续加工的矩阵。

一个混搭的.nc文件,同理txt或其他

下一步开始加工

首先

然后选中你想要导入的文件

最后依次选择元胞 创建函数 

这样就产生了一个导入的Funktion,但是你还需要自己输入一行代码,以我自己为例,我的函数是这样的

clc
clear all
readGcode = importfile("D:\Masterarbeit\programm\Gcode\readGcode.nc", [1, Inf]);
% readGcode是我想要导入元胞数组的那个在workspace中显示的数组名称
% 后面那一串是引用Function,在你创建Function以后,下面会有一段教你怎么做的

类似这样

function readGcode = importfile(filename, dataLines)
%IMPORTFILE Import data from a text file
%  READGCODE = IMPORTFILE(FILENAME) reads data from text file FILENAME
%  for the default selection.  Returns the data as a cell array.
%
%  READGCODE = IMPORTFILE(FILE, DATALINES) reads data for the specified
%  row interval(s) of text file FILENAME. Specify DATALINES as a
%  positive scalar integer or a N-by-2 array of positive scalar integers
%  for dis-contiguous row intervals.
%
%  Example:
%  readGcode = importfile("D:\Masterarbeit\programm\Gcode\readGcode.nc", [1, Inf]);
%
%  See also READTABLE.
%
% Auto-generated by MATLAB on 2022-01-12 15:31:50

%% Input handling

% If dataLines is not specified, define defaults
if nargin < 2
    dataLines = [1, Inf];
end

%% Set up the Import Options and import the data
opts = delimitedTextImportOptions("NumVariables", 5);

% Specify range and delimiter
opts.DataLines = dataLines;
opts.Delimiter = " ";

% Specify column names and types
opts.VariableNames = ["X41557", "Y18611", "VarName3", "VarName4", "VarName5"];
opts.VariableTypes = ["char", "char", "char", "char", "char"];

% Specify file level properties
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
opts.ConsecutiveDelimitersRule = "join";
opts.LeadingDelimitersRule = "ignore";

% Specify variable properties
opts = setvaropts(opts, ["X41557", "Y18611", "VarName3", "VarName4", "VarName5"], "WhitespaceRule", "preserve");
opts = setvaropts(opts, ["X41557", "Y18611", "VarName3", "VarName4", "VarName5"], "EmptyFieldRule", "auto");

% Import the data
readGcode = readtable(filename, opts);

%% Convert to output type
readGcode = table2cell(readGcode);
numIdx = cellfun(@(x) ~isnan(str2double(x)), readGcode);
readGcode(numIdx) = cellfun(@(x) {str2double(x)}, readGcode(numIdx));
end

 然后你的Workspace中就多了一个Cellarray,但是我们如何识别字母并加工他呢?

 

以我自己举例,由于G代码不同的大写字母有不同的涵义,所以我想先把他们归类,每一个大写字母归到一列内。

for i=2:row   %row
     for j=1:column
if strncmp(readGcode(i,j),'G',1)  %第一个字母为G 
    readGcodenew(i,1)=readGcode(i,j);%存入第一列
elseif strncmp(readGcode(i,j),'T',1)  %第一个字母为T
    readGcodenew(i,2)=readGcode(i,j); %存入第二列
elseif strncmp(readGcode(i,j),'M',1)%第一个字母为M
    readGcodenew(i,3)=readGcode(i,j);%存入第三列
elseif strncmp(readGcode(i,j),'S',1)%第一个字母为S
    readGcodenew(i,4)=readGcode(i,j);%存入第四列
elseif strncmp(readGcode(i,j),'F',1)%第一个字母为F
    readGcodenew(i,5)=readGcode(i,j);%存入第五列
elseif strncmp(readGcode(i,j),'X',1)%第一个字母为X
    readGcodenew(i,6)=readGcode(i,j);%存入第六列
elseif strncmp(readGcode(i,j),'Y',1)%第一个字母为Y
    readGcodenew(i,7)=readGcode(i,j);%存入第七列
elseif strncmp(readGcode(i,j),'Z',1)%第一个字母为Z
    readGcodenew(i,8)=readGcode(i,j);%存入第八列
elseif strncmp(readGcode(i,j),'z',1)%第一个字母为z
    readGcodenew(i,8)=readGcode(i,j);%存入第八列
else  readGcodenew(i,j)=readGcode(i,j);%跳过
end
end
end

然后我就得到了一个矩阵,每一列都代表了不同的大写字母,但是为什么从2开始呢,因为1是文件名并且开头带了个大写G,所以我从第二行开始,就没问题了。

到这里我们就得到了一个这样的元胞数组,我们可以看到,每一列都是一个大写字母,并且是按顺序排列的。 然后我又用了两行代码把他变成了一个无字母的矩阵,以便于后续加工。

%% make it a matrix
readGcodenew = cellfun(@(s)s(2:end), readGcodenew, 'UniformOutput' , false ) ;     % Entfernen von Buchstaben
readGcodenew = str2double( readGcodenew );   % Leere Klammern entfernen , Cell zu Matrix

我们可以看到,这里空白的地方都被NaN填充了,后续我们要把NaN变成0,这个时候就需要把矩阵内本身为0的值给特殊处理,以便于后续识别。这里我把本身为零的非坐标值变成了-1,本身为0的坐标值给了一个比较大的,永远不会用到的值。

%% G0,S0,F0,M0,T0 = -1
for i=1:row   %row
         if readGcodenew(i,1)== 0
             readGcodenew(i,1) = -1;
         elseif readGcodenew(i,2)== 0
             readGcodenew(i,2) = -1;
         elseif readGcodenew(i,3)== 0
             readGcodenew(i,3) = -1;
         elseif readGcodenew(i,4)== 0
             readGcodenew(i,4) = -1;
         elseif readGcodenew(i,5)== 0
             readGcodenew(i,5) = -1;
         elseif readGcodenew(i,6)== 0
             readGcodenew(i,6) = paipai;
         elseif readGcodenew(i,7)== 0
             readGcodenew(i,7) = paipai;
         elseif readGcodenew(i,8)== 0
             readGcodenew(i,8) = paipai;
         end
end 
readGcodenew(isnan(readGcodenew)==1) = 0;  % NaN zu 0

由于有很多的无用行,比如这里的0-9行,然后最后也有两个无用行,所以要自动识别有坐标的第一行以及最后一行,这里我是这么处理的

%% Z inizialisierung
for i=1:row
if readGcodenew(i,1) == -1   % Bestimmen die Startreihe
    readGcodenew(i,8) = Z0 ;   % Z-Wert inizialisieren
    firstline = i;
break
end
end

%% letzte linien
for i=1:row
if readGcodenew(i,1) == -1   % Bestimmen die Startreihe   
    lastline = i;
end
end

做完这些以后,还有一个问题,就是数值矩阵中,缺少的,为0的6,7,8列坐标值需要补足,因为在输出G代码时,默认坐标不变的话是不会显示的。

%% Koordinatenvererbung
Needtobeinherited = [1 4 5 6 7 8];
for i = firstline:row  %row
    for j = Needtobeinherited
   if     readGcodenew(i,j)== paipai
       readGcodenew(i,j) = 0;
   elseif readGcodenew(i,j)== 0
    readGcodenew(i,j) = readGcodenew(i-1,j);
   else   readGcodenew(i,j) = readGcodenew(i,j);
end
    end
end

然后我们就得到了一个,可以用来做很多事情的矩阵啦。对于我来说,还有很多后续的东西要做,比如识别内容,然后存入结构体,但是感觉比较特别,没啥用,所以就不写出来了,有需要的话我再写。 

PS:本人初学者,轻喷,很多地方应该都不对 哈哈哈

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值