'fopen' function error when placed inside of user defined function

3 ビュー (過去 30 日間)
Harry Carr
Harry Carr 2015 年 9 月 19 日
コメント済み: Image Analyst 2015 年 9 月 19 日
I am currently doing a question which requires me to write a function that has a file as the function input. The functions task is simply to open any file required containing just numbers and display it as a matrix. As I understand it, I can do this using the fopen and fscanf functions. This works fine, however when I try implement it as a function, I run into an error. This is what I have, first without it being inside my user function, which works, and second with it being inside my user created function which does not:
%#1
fid = fopen('1.txt');
x = fscanf(fid, '%f\n', [13, inf]);
%#2
function x = fun(file_name)
fid = fopen(file_name);
x = fscanf(fid, '%d\n', [13, inf]);
#2 Returns the error:
Error using fscanf
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in fun (line 3)
x = fscanf(fid, '%f\n', [13, inf]);
I'm really not sure how to fix this and any help would be much appreciated. (also I have included and if statement for if the file cannot be found but with or without this I still get the above error)
  2 件のコメント
Image Analyst
Image Analyst 2015 年 9 月 19 日
Can you attach the text file, or at least the first few lines of it as a file with the paperclip icon?
Harry Carr
Harry Carr 2015 年 9 月 19 日
Star Strider answered my question perfectly, but thank you anyway :)

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

採用された回答

Star Strider
Star Strider 2015 年 9 月 19 日
編集済み: Star Strider 2015 年 9 月 19 日
You don’t say how you call your function in your main program, and that would be useful to know. Your ‘file_name’ variable you send as a function argument has to be a string.
For instance:
file_name = '1.txt';
x = fun(file_name);
should work, assuming text file 1.txt exists in your MATLAB search path.
EDIT — Also consider adding the 'r+' permission:
fid = fopen(file_name, 'r+');
  3 件のコメント
Star Strider
Star Strider 2015 年 9 月 19 日
My pleasure!
Believe me, we’ve likely all done the same (I know I have), so you’re in quite good company!
Image Analyst
Image Analyst 2015 年 9 月 19 日
Don't quit with just that fix. It would still be a good idea to put in the more robust coding practices like I suggested, especially if you're going to write code that others will run without you being present.

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2015 年 9 月 19 日
Try this more robust code for your function:
if exist(file_name)
fid = fopen(file_name, 'rt');
if fid ~= -1
x = fscanf(fid, '%d\n', [13, inf]);
fclose(fid);
else
message = sprintf('Unable to open file for reading:\n%s\nPerhaps it is locked by another process.', file_Name);
uiwait(warndlg(message));
end
else
message = sprintf('File not found:\n%s', file_Name);
uiwait(warndlg(message));
end

カテゴリ

Help Center および File ExchangeWorkspace Variables and MAT Files についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by