getting error when using fscanf function

I am trying to read data from a file, read t and x, to solve the equation: x=A*exp(-a*t)+B*exp(-b*t),one of the terms would be neglected when t is too large, but i keep getting errors in the second line when i run the program. can I get some help?
this is the code:
fid=fopen('data1.txt');
A=fscanf(fid,'%f %f', [2 inf]);
[m n]=size(A);
t(1:n)=A(1,1:n);
x(1:n)=A(2,1:n);
Lx=log(x);
f=polyfit(t,Lx,1)
fclose(fid)

4 件のコメント

Walter Roberson
Walter Roberson 2013 年 3 月 13 日
What error are you getting?
Jan
Jan 2013 年 3 月 13 日
@Mohammed: It is a good idea to post the error message instead of only mentioning, that there is an error. While the forum is excellent in fixing problems, it is bad in guessing problems.
Mohamed
Mohamed 2013 年 3 月 13 日
編集済み: Image Analyst 2013 年 3 月 13 日
Thank you guys for your reply. This is the error message I am getting:
>> myfile.m
??? Error using ==> fscanf
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in ==> myfile at 2
A=fscanf(fid, '%f %f' , [2 inf]);
Thank you so much!
Image Analyst
Image Analyst 2013 年 3 月 13 日
編集済み: Image Analyst 2013 年 3 月 13 日
Evidently you chose not to use my code below, or follow my suggestion to write robust code. If you had used my code, you would not have gotten that error message.

サインインしてコメントする。

回答 (2 件)

Image Analyst
Image Analyst 2013 年 3 月 13 日

2 投票

You should endeavor to write more robust code (like I do). Here, try this:
folder = pwd; % Or 'C:/your data' or whatever it is.
fullFileName = fullfile(folder, 'data1.txt');
if exist(fullFileName, 'file')
errorMessage = sprintf('Error: file does not exist:\n%s', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
% File exists if you get here. Now try to actually open it.
fid=fopen(fullFileName);
if fid == -1
errorMessage = sprintf('Error: opening file:\n%s', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
% If you get to here then the file exists and you were able to open it.
A=fscanf(fid,'%f %f', [2 inf]);

2 件のコメント

Mohamed
Mohamed 2013 年 3 月 13 日
I am sorry, I didn't even see it before now, after you replied to my lasy post. Thank you so much, I will try this and let you know.
Image Analyst
Image Analyst 2013 年 3 月 14 日
If you want to run test7450data1.m, just type test7450data1 on the command line, not test7450data1.m because it thinks the .m means it's a structure field. I think that's what your error message in Friedrich's answer means.

サインインしてコメントする。

Friedrich
Friedrich 2013 年 3 月 13 日
編集済み: Friedrich 2013 年 3 月 13 日

1 投票

Hi,
who needs an error if one can guess ;)
I guess the file data1.txt doesn't exist or is located in a different folder. When MATLAB tries to open this non existing non findable file, it gives as file identifier -1 and the call to fscanf fails because of an invalid file identifier.
So take a look at your variable fid (3 or bigger is okay)

2 件のコメント

Mohamed
Mohamed 2013 年 3 月 14 日
編集済み: Image Analyst 2013 年 3 月 14 日
You are right,the file was in a different folder that the MATLAB folder. Despit of that I have to use the code that Image Analyst posted, because there is still this error message:
>> test7450data1.m
Warning: Direct access of structure fields returned by a function call (e.g.,
call to data1) is not allowed. See MATLAB 7.10 Release Notes, "Subscripting Into Function Return Values" for details.
??? Attempt to reference field of non-structure array.
Thanks!
Jan
Jan 2013 年 3 月 14 日
This is almost a complete error message. Only the line, which causes the problem is missing.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

質問済み:

2013 年 3 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by