Error in opening file from another folder, how to fix this problem?

2 ビュー (過去 30 日間)
Ekin
Ekin 2013 年 10 月 4 日
コメント済み: Ekin 2013 年 10 月 6 日
I have these codes to open and process a .txt file in MATLAB GUI:
[filename1,filepath1]=uigetfile({'*.txt*','Text Files'},...
'Select Data File');
cd(filepath1);
fp= fopen(filename1);
fgets(fp);
A = textscan(fp, '%f');
fclose(fp);
eee=A{:};
It works perfectly when opening a file in the same directory of the program, can open multiple files with same button. But when opening a second file in another folder directory, I get this error:
Undefined function 'myGUIprogram' for input arguments
of type 'struct'.
Error in
@(hObject,eventdata)myGUIprogram('pushbutton12_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
Can anybody help me fix it? Thanks in advance!
  2 件のコメント
dpb
dpb 2013 年 10 月 4 日
編集済み: dpb 2013 年 10 月 4 日
I've not tried it in a gui but per the doc's uigetfile needs must have the MultiSelect property set to On if want to return multiple files and indicates it's Off by default???
It's a cell array returned in that case, not a single string; not sure how to get the callback to handle the difference; I don't do GUIs so not best to ask; just noticed what looks to me like a shortcoming in the call.
But, I can't get it to allow multiple selection across two subdirectories at all -- I don't think that functionality is supported. I don't know about why the crash, though.
ADDENDUM: Or perhaps I read the question/problem description incorrectly; you mean a multi-select in a subdirectory other than the working directory, not from two separate subdirectories, maybe...well let's see what's different there -- nothing.
I'm coming to conclusion that the error isn't actually related to the above code--post the full error message in context with line numbers and all, not just the error text.
Ekin
Ekin 2013 年 10 月 6 日
Still multi select but not multi select at the same time, i mean when the button pushed, it loads a file; and push the button again and open another file. The problem is now solved. Thanks anyway.

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

採用された回答

Walter Roberson
Walter Roberson 2013 年 10 月 5 日
After you cd() to a directory and handle the files there, cd() back.
Alternately, put the directory that contains myGUIprogram.m into the MATLAB path instead of counting on it to be in the current directory.
Better yet, don't do the cd()
fp = fopen(fullfile(filepath1, filename1))
  1 件のコメント
Ekin
Ekin 2013 年 10 月 6 日
Removing cd and changing fp solved the problem, thanks Walter.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by