Effacer les filtres
Effacer les filtres

How can I print the full row where a specific string is found in a text file using MATLAB?

1 vue (au cours des 30 derniers jours)
Im searching for a string in a text file and printing it out. The output I get is all in one line and I don't know how to separate it so that it is on multiple lines.
My text file contains the following
Number Date Time
08-D-123519 02-Apr-2016 21:58:54
05-WD-3052 02-Apr-2016 22:03:350
08-D-123519 02-Apr-2016 21:58:54
05-WD-3052 02-Apr-2016 22:06:380
05-WD-3052 02-Apr-2016 22:18:560
08-D-123519 02-Apr-2016 21:58:54
05-WD-3052 02-Apr-2016 22:29:100
08-D-123519 02-Apr-2016 21:58:54
and I am and searching for the number and printing it out like so
tScan = textscan(fileID, '%s','Delimiter','');
tScan = tScan{:};
licenseP = ~cellfun(@isempty, strfind(tScan,'05-WD-3052'))
output = [tScan{licenseP}]
What I get
05-WD-3052 02-Apr-2016 22:03:35005-WD-3052 02-Apr-2016 22:06:38005-WD-3052 02-Apr-2016 22:18:56005-WD-3052 02-Apr-2016 22:29:100
What I'm trying to get
05-WD-3052 02-Apr-2016 22:03:350
05-WD-3052 02-Apr-2016 22:06:380
05-WD-3052 02-Apr-2016 22:18:560
05-WD-3052 02-Apr-2016 22:29:100

Réponse acceptée

Recap
Recap le 3 Avr 2016
The problem was that I was using the wrong parentheses. The solution is
output = vertcat(tScan{licenseP});

Plus de réponses (2)

Image Analyst
Image Analyst le 3 Avr 2016
How did you open the file? Did you use 'r' or 'rt'? If you used 'r', try 'rt' to read a text file.

Image Analyst
Image Analyst le 3 Avr 2016
When you do this:
tScan = tScan{:};
You're taking what was on separate lines ans stringing it all together as one string. So just don't do that and leave it as an array.
  1 commentaire
Recap
Recap le 3 Avr 2016
when I use
licenseP = ~cellfun(@isempty, strfind(tScan,'05-WD-3052'))
it doesnt work if i dont
tScan = tScan{:};

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by