How can I read this text file?

2 vues (au cours des 30 derniers jours)
Roberto Gomez
Roberto Gomez le 28 Mai 2018
Commenté : Roberto Gomez le 29 Mai 2018
I wrote this text file, then I wrote this code to read it and it gives me errors.
Error using dataread Trouble reading floating point number from file (row 1, field 2) ==> ,500,1000,1500,2200,2900,3600,4
Error in textread (line 168) [varargout{1:nlhs}]=dataread('file',varargin{:}); %#ok<REMFF1>
This is what I have, [t, Q]=textread('spherical.txt','%d %f');
Can someone help?

Réponse acceptée

Walter Roberson
Walter Roberson le 28 Mai 2018
... Is there a good reason to write in that particular format? There are other easier formats.
textread() was declared obsolete a fair number of years ago; you should probably not be using it for any new code (unless you are using a very old MATLAB release.)
S = fileread('spherical.txt');
parts = regexp(S, ';', 'split');
t = cell2mat(textscan(parts{1}, '%d', 'delimiter', ','));
Q = cell2mat(textscan(parts{2}, '%f', 'delimiter', ','));
Note: your t has 12 entries, but your Q only has 11 entries.
Are you sure you want your t variables to be int32 data type? It is possible, and even quite appropriate for some cases, but if you are not aware that it will be int32 then you can end up with problems in the computation. If you want double instead of int32 then use %f instead of %d. If you want 64 bit integers use %d64
  1 commentaire
Roberto Gomez
Roberto Gomez le 29 Mai 2018
Thanks, I fixed the entries to be the same. I will look into an alternative for textread().

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Tags

Produits


Version

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by