필터 지우기
필터 지우기

While Loop column vector of strings

조회 수: 2 (최근 30일)
Bob Whiley
Bob Whiley 2015년 2월 25일
답변: dpb 2015년 2월 25일
I am trying to make a column of names and a column of numbers given a .txt file. The names are always next to the number, with a ';' in between them. The way my code is set up, it looks like
fh = fopen('phoneList1.txt');
line = fgetl(fh);
vec = [];
numbers= [];
while (ischar(line))
[checkstuff, yak] = strtok(line, ';');
[blah, bleh] = strtok(yak, '(');
vec = [checkstuff vec];
numbers = [bleh numbers];
line = fgetl(fh);
end
fclose(fh);
But my vec output returns a one-by-one vector of all the names combined into one string with no spaces in between them, how can I give each name its own row?

답변 (1개)

dpb
dpb 2015년 2월 25일
If these are formatted phone numbers, they're text, too...
fh = fopen('phoneList1.txt');
c=textscan(fh,'%s %s','delimiter',';','collectoutput',1);
You'll have a cell array of Nx2, the first column will be the names the second the numbers.

카테고리

Help CenterFile Exchange에서 String Parsing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by