How do I load data files starting wih strings?
显示 更早的评论
For class I was assigned to create a script that would read a .dat file and plot the data contained within. The problem I am having is that the data file is formated as follows:
x 0 y 0
x 1 y 1
x 2 y 4
x 3 y 9
However when I attempt to load the file into the workspace to work with it MATLAB returns the following error:
clear, clc
load xypts.dat
Error using load
Unknown text on line number 1 of ASCII file xypts.dat
"x".
Error in HW_9_13 (line 2)
load xypts.dat
I have read elsewhere that ASCII files connot contain strings, does this mean I am approaching the problem wrong? Is there another way to get the neccessary data into the workspace?
采纳的回答
更多回答(1 个)
Stephen23
2019-3-12
1 个投票
>> str = fileread('temp.txt');
>> str = regexprep(str,'\s+','');
>> mat = reshape(sscanf(str,'x%fy%f'),2,[]).'
mat =
0 0
1 1
2 4
3 9
类别
在 帮助中心 和 File Exchange 中查找有关 Standard File Formats 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!