# Read in only certain numbers from a txt file.

조회 수: 17(최근 30일)
John Kramer 2021년 5월 11일
답변: Scott MacKenzie 2021년 5월 11일
Hello All,
I have a bunch of text files where i only need to extract the 11 digit numbers from the txt file. I am currently struggling to only pull those numbers out. I am able to pull out either all the data, all the numbers but not the exact numbers I want. I attached a example of what the text documents I am trying to extract from. I am basically just trying to create a cell array of the 11 digit numbers.
Thanks
J

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

### 채택된 답변

Scott MacKenzie 2021년 5월 11일
This creates a cell array of the 11-digit numbers in your file...
fileName = 'Example.txt';
n = [];
fid = fopen(fileName);
while ~feof(fid)
line = fgetl(fid);
if length(line) == 11
n = [n; str2double(line) ];
end
end
fclose(fid);
c = num2cell(n)
Output:
c =
9×1 cell array
{[12345678901]}
{[12345678902]}
{[12345678903]}
{[12345678904]}
{[12345678905]}
{[12345678906]}
{[12345678907]}
{[12345678908]}
{[12345678909]}

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

### 추가 답변(2개)

Bob Thompson 2021년 5월 11일
What does your code look like so far? What are you using to extract the numbers?
Off the top of my head, I recommend using something like regexp to extract the numbers. Here's a quick swag at what it would look like:
11nums = regexp('Example.txt','/D(/d/d/d/d/d/d/d/d/d/d/d)/D','tokens');
This has not been tested.
In theory it looks for an isolated set of numbers, with 11 digits.
##### 댓글 수: 0표시숨기기 이전 댓글 수: -1

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

Thomas Jensen 2021년 5월 11일
Hi John,
Unfotunatelly your input file does not follow a pattern, so your script needs to read line-by-line and check if the line is an array of characters containing 11 digits.
Best regards,

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

### 범주

Find more on Characters and Strings in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by