Whitespace Delimited Textfile, NaN results?
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Sage Hayes
il 28 Ott 2021
Risposto: Walter Roberson
il 28 Ott 2021
Hi, so I'm having trouble reading this delimited textfile. It's whitespace delimited columns with rows being indicated by carriage returns.
So far I've been trying to read it through this method:
clear all;
close all;
fid = fopen('test.txt', 'rt');
%make headers
tline = fgetl(fid);
headers = strsplit(tline, ','); %a cell array of strings
%make data
datacell = textscan(fid,'%f%f%f%f', 'Delimiter',' ', 'CollectOutput', 1);
fclose(fid);
datavalues = datacell{1}; %as a numeric array
However this has resulted in a bunch of NaN's inserted into the matrix (Capture.PNG).
Any help would be greatly appreciated.
0 Commenti
Risposta accettata
per isakson
il 28 Ott 2021
Modificato: per isakson
il 28 Ott 2021
The problem is caused by 'Delimiter',' ' in
datacell = textscan(fid,'%f%f%f%f', 'Delimiter',' ', 'CollectOutput', 1);
in combination with two spaces between the third and the fourth column in the text file.
Try
datacell = textscan( fid,'%f%f%f%f', 'CollectOutput', 1);
and let Matlab take care of the problem or
datacell = textscan(fid,'%f%f%f%f', 'Delimiter',' ', 'CollectOutput',1, 'MultipleDelimsAsOne',true);
Più risposte (1)
Walter Roberson
il 28 Ott 2021
filename = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/781593/test.txt';
datavalues = readmatrix(filename)
0 Commenti
Vedere anche
Categorie
Scopri di più su Text Files in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!