Reading numbers from alphanumeric strings in a cell matrix
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi ppl, i am reading a text file consisting of various lines of alpha-numeric strings...
N010 G00 X0 Y0 Z0 N020 G01 X-10 Y10 Z-1 N030 G01 X20 Y40 Z-1 N040 G04 X3 Y3 Z10 M03
Now I need all numerical values of X, i.e. -10,20,3. The problem I am facing is that the strings are read from text file as 'cell' vector and i have to convert them to 'char' to use 'regexp' using '\w*X\w*'....this works, but does not allow the negative number to be read because it starts with a '-'. What to do now??? can anybody help?? I am using MATLAB 2010
댓글 수: 3
채택된 답변
Matt Fig
2012년 10월 13일
X = {'N010' 'G00' 'X0' 'Y0' 'Z0' 'N020' 'G01'...
'X-10' 'Y10' 'Z-1' 'N030' 'G01' 'X20' 'Y40'...
'Z-1' 'N040' 'G04' 'X3' 'Y3' 'Z10' 'M03'};
N = cellfun(@str2double,regexp([X{:}],'(?<=X)-*\d+','match'))
댓글 수: 3
Matt Fig
2012년 10월 13일
If you have:
X = ['N010 G00 X0 Y0 Z0 N020 G01 X-10 Y10',...
'Z-1 N030 G01 X20 Y40 Z-1 N040 G04 X3 Y3 Z10 M03']
Then simply use:
N = cellfun(@str2double,regexp(X,'(?<=X)-*\d+','match'))
추가 답변 (1개)
Azzi Abdelmalek
2012년 10월 13일
편집: Azzi Abdelmalek
2012년 10월 13일
A={'N010' 'G00' 'X0' 'Y0' 'Z0' 'N020' 'G01' 'X-10' 'Y10' 'Z-1' 'N030'}
y=cell2mat(cellfun(@(x) str2num(cell2mat(regexp(x,'-?[0-9]','match'))),A,'un',0))
댓글 수: 3
Azzi Abdelmalek
2012년 10월 13일
A={' N010 G00 X0 Y0 Z0 N020 G01 X-10 Y10 Z-1 N030 G01 X20 Y40 Z-1 N040 G04 X3 Y3 Z10 M03'}
y=regexp(x,'-?[0-9,.]','match')
out=cell2mat(cellfun(@(x) str2num(cell2mat(x)),y,'un',0))
참고 항목
카테고리
Help Center 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!