How can I read this text file?

조회 수: 2 (최근 30일)
Roberto Gomez
Roberto Gomez 2018년 5월 28일
댓글: Roberto Gomez 2018년 5월 29일
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?

채택된 답변

Walter Roberson
Walter Roberson 2018년 5월 28일
... 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
Roberto Gomez
Roberto Gomez 2018년 5월 29일
Thanks, I fixed the entries to be the same. I will look into an alternative for textread().

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by