How do I load data files starting wih strings?
Mostra commenti meno recenti
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?
Risposta accettata
Più risposte (1)
Stephen23
il 12 Mar 2019
1 voto
>> 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
Categorie
Scopri di più su Standard File Formats in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!