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

2 次查看(过去 30 天)
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))

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by