unable to open a text file from different loacations

1 回表示 (過去 30 日間)
shanmukh
shanmukh 2013 年 2 月 20 日
i am using the following code to get the .txt file from any location
a1 = uigetfile('../Data/*.txt');
fid = fopen(a1,'r+');
i am able to select .txt files which are present only in matlab working folder. if i place the same file on desktop and try to select it i get an error as shown below
Error using textscan
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in text_fin1 (line 7)
c = textscan(fid,'%d64%f64%s%s%d64%d64%d64%d64%d64','delimiter',
';','HeaderLines',256);
please let me know why i have this error, how to remove it

回答 (1 件)

Jan
Jan 2013 年 2 月 20 日
編集済み: Jan 2013 年 2 月 20 日
uigetfile replies the path and the file name. You have to use both to address your file:
[filename, pathname] = uigetfile('*.txt');
fid = fopen(fullfile(pathname, filename), 'r+');
Otherwise Matlab searches the file in the current folder. For fopen(name, 'r') Matlab would even search in the complete Matlab path, which might lead to very unexpected effects. But fortunately the write-permissions triggered by 'r+' let fopen be less smart.
  1 件のコメント
shanmukh
shanmukh 2013 年 2 月 20 日
i used the following code
a1 = uigetfile('*.txt');
fid = fopen(fullfile(a1), 'r+');
no use it gives me the same error

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

カテゴリ

Help Center および File ExchangeData Import and Export についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by