×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

read data file

read data file

read data file

(OP)
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

RE: read data file

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.

RE: read data file

(OP)
Hi, Jockek:
Thanks for the help. But I tried the code you gave me, The result still shows ''. Any more suggestions?

bj

RE: read data file

(OP)
Also I can use "fgetl(fid)" to get the data from each line.
just not able to use "fscanf" command.

bj

RE: read data file

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
www.vibrationdata.com

RE: read data file

(OP)
Hi, Tom:
I tried your program, and it worked!!
Really appreciated.

bj

RE: read data file

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.

RE: read data file

(OP)
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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources