Can uigetfile function be used to return file name & location for a file that is already open?
4 次查看(过去 30 天)
显示 更早的评论
I am using MATLAB's App Designer and I would like to allow the user to press a button and select a file. Then I would like to read/write to that same file. In some cases, I would like to do this for files that are already open.
I'm using the following to read/write to an open excel file.
excel = actxGetRunningServer('Excel.Application')
val = cell2table(excel.Workbooks.Open(filename))
I'd like to allow the user to find the file path and name by searching for it instead of manually entering it:
[file, location] = uigetfile({'*.xlsm'; '*xlsx'}, 'File Selector');
However, uigetfile() gives the attached error. Is there a way of allowing this function to work on already open files?
Thanks.
0 个评论
回答(2 个)
dpb
2025-7-9
编辑:Walter Roberson
2025-7-9
I cannot reproduce the symptom here. uigetfile returns the filename and path as expected even if the workbook is open in an ActiveX session...
>> fn=fullfile(pwd,'ClausSample.xlsx')
fn =
'C:\Users\Duane\Documents\MATLAB\Work\ClausSample.xlsx'
>> wbk=excel.Workbooks.Open(fn)
wbk =
Interface.000208DA_0000_0000_C000_000000000046
>> [f,p]=uigetfile('*.xlsx','Open File','ClausSample.xlsx')
f =
'ClausSample.xlsx'
p =
'C:\Users\Duane\Documents\MATLAB\Work\'
>> excel.ActiveWorkbook.Name
ans =
'ClausSample.xlsx'
>> excel.ActiveWorkbook.Close(0)
That error must come from later on trying to do something else to the already open file in a second process.
You cannot subsequently open the file again if ActiveX has it already open, but uigetfile only returns the file name, it doesn't do anything to the file itself.
2 个评论
dpb
2025-7-9
编辑:dpb
2025-7-9
Probably not. I avoid it like the plague it is...
However, the following should be workable...
cmd='explorer "C:Users\UserName\OneDrive\"'
system(cmd);
to open FileExplorer at the chosen point in the OneDrive folder.
Unlike uigetfile, if pass a fully-qualfied filename, explorer goes ahead and opens the file instead of just returning the name.
Enjoy...
Image Analyst
2025-7-10
What other process opened the file you are trying to select with uigetfile()?
What kind of functions are you planning on using if you were able to open it? Generally I don't think it's a good idea to have two different processes working on the same file, especially if they are not aware of what the other process is doing? Sometimes a process can put a lock on a file so that no other program can mess with it until it's done. Other times programs don't lock the file for editing (for example Notepad).
I almost never use uigetfile. I think it's tedious and not convenient for my users. I automatically load my files into a listbox on my GUI, for example in the startupfcn function. I use dir to get the file list to load into the Items property of the listbox. Let me know if you need to know how to do that with App Designer. Then the user simply clicks on the filename in the listbox. You can then get the filename they clicked on and do something with it.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Environment and Settings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!