I have a text file with these contents:

GULBERG (source)
MODEL TOWN (destination)

I want to extract GULBERG and MODEL TOWN (or any other names that appear here) and discard (source) and (destination) from the file using Matlab. Further, I'm saving these names to variables STRING{1},STRING{2} for later use.

But the problem i'm facing is that my code extracts only "GULBERG" and "MODEL" from the file and not the second word i.e "TOWN".

My output so far:

GULBERG
MODEL

How can i fix this so that i get the word TOWN as well in the output??

Here's my code:

fid = fopen('myfile.txt');

thisline = fgets(fid);
a=char(40); %character code for paranthesis (
b=char(41); %character code for paranthesis )
STRING=cell(2,1);
ii=1;

while ischar(thisline)
  STRING{ii}=sscanf(thisline,['%s' a b 'source' 'destination']);
  ii=ii+1;
  thisline = fgets(fid);
end

fclose(fid);

% STRING{1} contains first name 
% STRING{2} contains second name
有帮助吗?

解决方案

Assuming that the identifiers - (source) and (destination) always appear at the end of the lines after the town names that are to be detected, see if this works for you -

%%// input_filepath and output_filepath are filepaths 
%%// of input and output text files

str1 = importdata(input_filepath)

split1 = regexp(str1,'\s','Split')

%%// Store row numbers that do not have (source) or (destination) as a string
ind1 = ~cellfun(@isempty,(regexp(str1,'(source)'))) | ...
    ~cellfun(@isempty,(regexp(str1,'(destination)')));

str1 = strrep(str1,' (source)','')
str1 = strrep(str1,' (destination)','')

STRING = str1(ind1,:)

%%// Save as a text file
fid = fopen(output_filepath,'w');
for k = 1:size(STRING,1)
    fprintf(fid,'%s\n',STRING{k,:});
end
fclose(fid);

其他提示

While I was waiting for an answer, i did more digging myself and found the solution to my problem. Looks like using strrep() to replace the unwanted words with '' solved my problem. I'm sharing this so anyone with a similar problem might find this helpful!

Here's what I did:

fid = fopen('myfile.txt');
thisline = fgets(fid);
a=char(40);
b=char(41);
STRING=cell(2,1);
index=1;
while ischar(thisline)
STRING{index} = strrep(thisline,'(source)','');
 index=index+1;
    thisline = fgets(fid);
end
STRING{2} = strrep(STRING{2},'(destination)','');
fclose(fid);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top