필터 지우기
필터 지우기

fopen problem How does it work?

조회 수: 3 (최근 30일)
André Luiz Regis Monteiro
André Luiz Regis Monteiro 2014년 2월 10일
댓글: Image Analyst 2014년 2월 11일
I have a file VarName3.txt like this:
[2.281000003
2.593500003
2.749750003
3.062250003
3.062250003...
and negative values too. It is 1x1000.
The code I am using is:
fid=fopen('VarName3.txt','r');
atensao=fread(fid,'float');
fclose(fid);
When I type in Comand Window to check atensao, the answer is:
1.0e-03 *
0.0425
0.0000
0.0000
0.0006
0.0000...
Why it doesn't work? What am I doing wrong? Could someone teach me? Thanks a lot.

채택된 답변

dpb
dpb 2014년 2월 10일
fid=fopen('VarName3.txt','r');
atensao=fread(fid,'float');
fclose(fid);
Surprising got anything that reasonable. If the input file is actually an ASCII text file, then use fscanf instead of fread. fread is for stream unformatted data, not formatted.
fid=fopen('VarName3.txt','r');
atensao=fscanf(fid,'%f');
fclose(fid);
Or use one of the higher-level routines--while deprecated by TMW, textread is awfully handy for such simple cases as it wraps the fopen/fclose in the call and returns an array rather than a cell for the much fancier textscan
atensao=textread('VarName3.txt');
even allowing to dispense with the format string for simple numeric data such as your case. See
doc textread
doc textscan
doc fscanf
for more details
  댓글 수: 2
André Luiz Regis Monteiro
André Luiz Regis Monteiro 2014년 2월 11일
편집: André Luiz Regis Monteiro 2014년 2월 11일
Thank you "dpb". I really appreciated. But when I check "atensao" it shows me number like: 2.2810 instead of 2.281000003 (from original file VarName3.txt). Do you know how can I fix it? Thank you again.
Image Analyst
Image Analyst 2014년 2월 11일

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2014년 2월 10일
If you are reading text you should not be using fread() which is for reading binary files.
Have a look at fscanf(). Also have a look at textscan()
  댓글 수: 1
André Luiz Regis Monteiro
André Luiz Regis Monteiro 2014년 2월 11일
Thank you Walter Roberson. I used first option.

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

카테고리

Help CenterFile Exchange에서 Data Import and Export에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by