Can uigetfile function be used to return file name & location for a file that is already open?
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (2)
dpb
el 9 de Jul. de 2025
Editada: Walter Roberson
el 9 de Jul. de 2025
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 comentarios
dpb
el 9 de Jul. de 2025
Editada: dpb
el 9 de Jul. de 2025
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
el 10 de Jul. de 2025
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 comentarios
Ver también
Categorías
Más información sobre Environment and Settings en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!