1

To delete rows with a specified string, I read

http://www.mathworks.com/matlabcentral/answers/34567-remove-row-with-matching-string

I did but failed.

text.txt looks like

123     A omit B
2323    C omit D
333         oh

And I coded

fid = fopen('text.txt');
tttt = textscan(fid, '%s %s','delimiter','\t','HeaderLines',0)
tttt(strcmp(tttt{2}, 'omit'), :) = []
strcmp(tttt{2}, 'omit')

But MatLab showed me "Empty cell array: 0-by-3" and

ans =

     0
     0
     0

I guess the type of file "tttt" is wrong (because I had to write "tttt{2}" rather than tttt(:,2)) but I am not sure.

user1849133
  • 527
  • 1
  • 7
  • 18
  • Replace `strcmp` with `findstr`? You'll have to edit your code slightly as well as the function looks slightly different http://www.mathworks.com/help/matlab/ref/findstr.html – Dan Jul 05 '13 at 13:59
  • @Dan Replacing with findstr didn't succeed. Basically, how can I import the file correctly so that I can apply the code at "mathworks.com/matlabcentral/answers/…; ? If I apply that code to my case, tttt(:, 2) should work. But it doesn't. The row-column relationship seems to be broken when importing the text.txt – user1849133 Jul 05 '13 at 14:18

2 Answers2

3

You are using strcmp(), which compares the equality of two strings. That is not what you are after; you want to check whether some string is a substring of another string. You can use strfind or findstr or regexp or regexprep for that purpose.

ALso, you are not closing your text file. This can lead to all sorts of problems. Make it a habit to always write fid = fopen(...); fclose(fid); as if it is one command, and then continue coding in between.

A small example:

fid = fopen('text.txt');

    tttt = textscan(fid, '%s %s','delimiter','\t');
    tttt{2}(~cellfun('isempty', strfind(tttt{2}, 'omit'))) = [];

fclose(fid);

EDIT: based on your comments, I think you want to do this:

fid = fopen('text.txt');

% Modified importcommand
tttt = textscan(fid, '%s%s',...
    'delimiter','\t',....
    'CollectOutput', true);
tttt = tttt{1};

% Find rows for deletion 
inds = ~cellfun('isempty', strfind(tttt(:,2), 'omit'));

% aaaand...kill them!    
tttt(inds,:) = [];


fclose(fid);
Rody Oldenhuis
  • 37,726
  • 7
  • 50
  • 96
  • Thank you but it was text.txt rather than test.txt :) And the delimiter should be \t. And I want to whole row if "omit" is included. If (2,2) contains "omit", (2,1) and (2,2) should be deleted. Your code didn't look to do that but might give me some hints.. – user1849133 Jul 05 '13 at 14:15
  • @Rody_Oldenhuis Basically, how can I import the file correctly so that I can apply the code at "http://www.mathworks.com/matlabcentral/answers/34567-remove-row-with-matching-string" ? If I apply that code to my case, tttt(:, 2) should work. But it doesn't. The row-column relationship seems to be broken when importing the text.txt – user1849133 Jul 05 '13 at 14:17
  • @user1849133: see my latest edit (I hate tabs, so I temporarily changed to comma :)) Now, by "whole row" you mean to delete the corresponding row in `tttt{1}` as well? They are different entities, so that's an important distinction. – Rody Oldenhuis Jul 05 '13 at 14:18
  • @user1849133: OK, see my latest edit again. I think that's what you're after :) – Rody Oldenhuis Jul 05 '13 at 14:24
1

If performance is a concern, you might try invoking Unix's sed directly from within Matlab:

system('sed ''/omit/d'' text.txt > output.txt');

Here is the analogous command, calling AWK from within Matlab:

system('awk ''!/omit/'' text.txt > output.txt');
Community
  • 1
  • 1
supyo
  • 3,017
  • 2
  • 20
  • 35
  • It says "sed is not an internal or external command or executable program or batch file." "awk is not an internal or external command or executable program or batch file." – user1849133 Jul 07 '13 at 07:22
  • This assumes you have a *nix operating system, or Cygwin etc. installed on Windows. The executables for awk and sed are typically at /usr/bin/awk and /usr/bin/sed. You will need to make sure these are on your path. – supyo Jul 08 '13 at 11:33