Filter löschen
Filter löschen

While Loop column vector of strings

2 Ansichten (letzte 30 Tage)
Bob Whiley
Bob Whiley am 25 Feb. 2015
Beantwortet: dpb am 25 Feb. 2015
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?

Antworten (1)

dpb
dpb am 25 Feb. 2015
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.

Community Treasure Hunt

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

Start Hunting!

Translated by