reading from a text file
1 view (last 30 days)
Show older comments
hello everyone,
i am trying to read from a text file which has the following format
X: 549.123 Y: -100.235 Z: -230.89
X: 550.123 Y: -101.235 Z: -231.89
X: 551.123 Y: -102.235 Z: -232.89
X: 552.123 Y: -103.235 Z: -233.89
X: 553.123 Y: -104.235 Z: -234.89
the problem is when i read using textscan it stores the data in 3*10 cells where the first cell format is in the first column X: and in second column 549.123.
i want to store data in 3 matrices each of 1*5 ?
eagerly waiting for a reply.
0 Comments
Accepted Answer
Thomas
on 25 Sep 2012
Edited: Thomas
on 25 Sep 2012
Consider you have your data in file doc.txt, your ouput matrices are x_val,y_val, and z_val
fileID = fopen('doc.txt');
C = textscan(fileID, '%s %f32 %s %f32 %s %f32');
fclose(fileID);
x_val=cell2mat(C(:,2))
y_val=cell2mat(C(:,4))
z_val=cell2mat(C(:,6))
Output is
x_val =
549.12
550.12
551.12
552.12
553.12
y_val =
-100.24
-101.24
-102.24
-103.24
-104.24
z_val =
-230.89
-231.89
-232.89
-233.89
-234.89
doc.txt is as follows
X: 549.123 Y: -100.235 Z: -230.89
X: 550.123 Y: -101.235 Z: -231.89
X: 551.123 Y: -102.235 Z: -232.89
X: 552.123 Y: -103.235 Z: -233.89
X: 553.123 Y: -104.235 Z: -234.89
0 Comments
More Answers (0)
See Also
Categories
Find more on Text Files 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!