using regexp for char data

out1 =
p.11 109 820 109
p.12 503 159 117
p.13 234 207 151
p.14 139 134 215
Name Size Bytes Class Attributes
out1 1x188 376 char global
%when I want to exctract first column of this data I use
out1 = char(regexp(out1,'p\.\d+','match'))
%the problem is first column characters varies. for example out1 would be;
out1 =
o10 109 820 109
o12 503 159 117
o13 234 207 151
o14 139 134 215
%or it would be
out1 =
oa10 109 820 109
ob12 503 159 117
oc13 234 207 151
od14 139 134 215
%as you seen, it changes so I need to use regexp without dependent first character or numbers belongs to first column.
thanks in advance

답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 7월 10일

0 개 추천

a=cellfun(@(x) regexp(x,'\d+','match'),out1,'un',0);
for k=1:numel(a)
out(k,1)=str2num(char(a{k}(1)))
end

댓글 수: 3

sermet
sermet 2013년 7월 10일
편집: Azzi Abdelmalek 2013년 7월 10일
it doesnt't work I give you initial codes;
cellArray=cellstr(cellArray)
out1 = sprintf('%s\n', cellArray{:})
a=cellfun(@(x) regexp(x,'\d+','match'),out1,'un',0)
for k=1:numel(a)
out(k,1)=str2num(char(a{k}(1)))
end
%it gives
Error using cellfun
Input #2 expected to be a cell array, was char instead.
Error in helmert_fig>pushbutton1_Callback (line 146)
a=cellfun(@(x) regexp(x,'\d+','match'),out1,'un',0)
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in helmert_fig (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)helmert_fig('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
error
Jan
Jan 2013년 7월 10일
It seems like out1 is a string, which contain line breaks: "1x188 char". Do you assume it is a cell string.
sermet
sermet 2013년 7월 10일
yes I do

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

질문:

2013년 7월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by