Importing only specific entries from a text file to matlab

조회 수: 2 (최근 30일)
Krish
Krish 2013년 3월 21일
Hello,
I am trying to import a text file, but do not need all the information, just only specifics (after the : operator). Here is a sample version of the text file
Select type (A or B or C): C Enter the number of days: 30 Select test type (Reg or Mod): Reg Enter the number of TC's used: 1
The data is a mix of alphanumeric and numeric. So I need something that handle both. Any help will be appreciated. Thanks

채택된 답변

Cedric
Cedric 2013년 3월 21일
편집: Cedric 2013년 3월 21일
Regular expressions are probably your best option. Look at the following:
>> s = 'Select type (A or B or C): C Enter the number of days: 30 Select test type (Reg or Mod): Reg Enter the number of TC''s used: 1' ;
>> results = regexp(s, '(?<=:\s+)\S+', 'match')
results =
'C' '30' 'Reg' '1'
The second argument in the call to REGEXP is the pattern. Let me know if you need help to understand it.
  댓글 수: 6
Krish
Krish 2013년 3월 22일
Got it. Sure make a difference when you have someone to take the time to explain. Its hard to figure something this simple when you have to read it from a text by yourself.
Again thanks a lot for your time and effort.
Cedric
Cedric 2013년 3월 22일
You're welcome!
Indexing is not the simplest thing to understand by ourselves actually, this is why I gave a little more explanations than usual. Paradoxically, understanding how to solve nice enough differential equations with MATLAB is simpler than understanding basic data structure and indexing.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by