Reputation: 3
I want to take the second column of a real time changing text file by textscan command. I can only take the first column of the text file but I want the second column. Here is my code:
fileid = fopen (Path);
rxt = textscan (fileid, '%d %*[^\n]' );
fclose (fileid);
arr = rxt {1,1};
How can I modify this for reading only second column?
Here is some portion from text file:
226, -187500,02, -187500,02, -187500,02, -187500,02, -187500,02, -187500,02, -187500,02, -187500,02, 0,00, 0,00, 0,00
227, -187500,02, -187500,02, -187500,02, -187500,02, -187500,02, -187500,02, -187500,02, -187500,02, 0,00, 0,00, 0,00
228, -187500,02, -187500,02, -187500,02, -187500,02, -187500,02, -187500,02, -187500,02, -187500,02, 0,00, 0,00, 0,00
Upvotes: 0
Views: 108
Reputation: 65430
You can specify that you'd like to discard the first number (%*d
), keep the second (%d)
, and discard the rest of the string (%*[^\n]
)
textscan(fileid, '%*d, %d, %*[^\n]')
Based on the data though, you've pasted a comma-separted file that also uses ,
for the radix point. As such, you'll want to read in the second and third numbers and convert them into a floating point number
numbers = textscan(fileid, '%*d, %d,%d %*[^\n]');
numbers = arrayfun(@(a,b)a * 10^b, numbers{:})
Upvotes: 2