Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

read data file

Status
Not open for further replies.

bjbdts

Electrical
Sep 1, 2005
58
0
0
US
Hi,
I am trying to read a .txt data file. But somehow can only get "''". Below is my code of write file, and then read file.

%write data:
x=0:0.1:1;
y=[x;exp(x)];
fid=fopen ('C:\exptab.txt', 'w');
%fprintf(fid,'Exponential function\n');
fprintf(fid, '%6.2f %12.8f\n',y);
fclose (fid);
%read data
fid=fopen('C:\exptab.txt','r');
AA=fscanf(fid,'%6.3f %13.2f\n');
fclose(fid);

Can anybody please to help me find the error?
Thanks,

bj
 
Replies continue below

Recommended for you

The documentation on fscanf contains an example of this.
Use
Code:
doc fscanf
and you'll see that
Code:
fscanf(fid,'%f %f',[2 inf]);
will do the trick.
 
Hi, Jockek:
Thanks for the help. But I tried the code you gave me, The result still shows ''. Any more suggestions?

bj
 
Here the code that I use to read in a file with two columns of data. The first column is assigned to variable t, the second to variable y.


%
disp(' ')
disp(' Select file input method ');
disp(' 1=external ASCII file ');
disp(' 2=file preloaded into Matlab ');
file_choice = input('');
%
if(file_choice==1)
[filename, pathname] = uigetfile('*.*');
filename = fullfile(pathname, filename);
%
fid = fopen(filename,'r');
THM = fscanf(fid,'%g %g',[2 inf]);
THM=THM';
else
THM = input(' Enter the matrix name: ');
end
%
t=double(THM:),1));
y=double(THM:),2));
%

Tom Irvine
 
If you had bothered to read the docs, as I suggested, you would have seen an example of the fscanf function. Where the use of the fscanf is exactly the same as in Tom's post.
 
Hi Jockek:
I actually read the docs and copied the example of the fscanf function to my program, but it didn't work. The result always show "''".That's why I posted my question here. I'm not the one you implied that I didn't spend time to read documents, and just wanted a quick answer.BTW, I haven't figured out why my original program show the '' result. I will try to understand and if I do, I will post the reason here.

Regards,

bj
 
Status
Not open for further replies.
Back
Top