How to remove rows with any string from matrix

Hello,
I'm trying to remove any rows that have any strings in them in my matric, for example:
name and other jibberish
1 0
0 1
0 2
another name and other words
0 3
1 0
other tosh
and change this to just:
1 0
0 1
0 2
0 3
1 0
so you can see, it doesn't matter what the string is, its not specific to what the letters in the string are, I just want it removed. Is there an easy way of doing this?
Thanks

댓글 수: 3

Is your matrix a cell array? Or are you trying to import a text file?
I'm trying to import a text file, which in itself is causing me a problem as the rows with strings have different column lengths than the rows with values which have the same column lengths!
For this, my solution should work.

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

 채택된 답변

Jos (10584)
Jos (10584) 2014년 6월 25일
편집: Jos (10584) 2014년 6월 25일

0 개 추천

Assuming that the rows are lines of a text file:
T = textread('data.txt','%s','delimiter','\n')
T2 = T(~cellfun(@(x) any(isletter(x)),T)) % still strings
VAL = str2num(char(T2)) % numbers

댓글 수: 1

This works well. But a note to anyone who wants to use this.. makes sure your value do not contain E (obviously) in replacement of decimal places.
Thank you

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

추가 답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2014년 6월 25일
편집: Azzi Abdelmalek 2014년 6월 25일

0 개 추천

fid = fopen('file.txt');
res={};
while ~feof(fid)
res{end+1,1} =fgetl(fid);
end
fclose(fid);
res(cellfun(@(x) any(isletter(x)),res))=[]
out=cell2mat(cellfun(@str2num,res,'un',0))

카테고리

도움말 센터File Exchange에서 Cell Arrays에 대해 자세히 알아보기

질문:

2014년 6월 25일

댓글:

2014년 6월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by