MATLAB: Fopen problem How does it work

fopenfreadMATLAB

I have a file VarName3.txt like this:
[2.281000003
2.593500003
2.749750003
3.062250003
3.062250003...
and negative values too. It is 1×1000.
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.

Best Answer

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