fopen works fine in *.m but fails in compiled Matlab

5 ビュー (過去 30 日間)
Brian
Brian 2015 年 12 月 14 日
コメント済み: Brian 2015 年 12 月 15 日
I use something like:
fpCSV = fopen(NameOfFile, 'rt');
fgets(fpCSV);
lineStr = fgets(fpCSV);
fclose(fpCSV);
in my *.m file. It is actually buried in the method of a class that I wrote.
In interactive mode in Matlab, the script works great. I ran the Application Builder Tool and successfully compiled. However, when I run my application I get "Invalid file identifier" (see attached screenshot). How do I debug this?
  2 件のコメント
Brian
Brian 2015 年 12 月 14 日
  • Compiler: Visual Studio 2012
  • Matlab Version: R2014a
File name acquired using:
NameOfFile = uigetfile('*.csv');
Brian
Brian 2015 年 12 月 15 日
Solved...
Sorry for the waste of time... Need to include the complete path with
[NameOfFile, NameOfPath] = enduigetfile('*.csv');

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

採用された回答

Guillaume
Guillaume 2015 年 12 月 14 日
One important rule when dealing with user input, filesystem or anything else that is external to your program is to always make sure that it conforms to your expectations. In your particular case, that means checking that the file is actually open, i.e. check the value of fpCSV after the fopen call.
Another good practice is to only deal with absolute paths instead of relying on the current folder. I strongly suspect that is your problem here. The current directory is probably not the same in your executable as it is in interactive mode, and the file simply isn't in that directory. So:
[NameOfFile, PathOfFile] = uigetfile('*.csv');
[fpCSV, errmsg] = fopen(fullfile(PathOfFile, NameOfFile), 'rt');
if fpCSV == -1
error('failed to open file %s because %s', NameOfFile, errmsg);
end

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB Compiler についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by