Can uigetfile function be used to return file name & location for a file that is already open?

4 visualizzazioni (ultimi 30 giorni)
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.

Risposte (2)

dpb
dpb il 9 Lug 2025
Modificato: Walter Roberson il 9 Lug 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 Commenti
McCain
McCain il 9 Lug 2025
I paused execution of the code directly after calling the uigetfile function for debugging purposes and still get the error, so I don't think it can be a result of any later processes.
The file I am trying to locate using uigetfile() is stored on OneDrive rather than my local machine, so I believe that is the issue. I saved a local copy and the function worked as expected.
Is there any way to make this work for a file stored on OneDrive?
dpb
dpb il 9 Lug 2025
Modificato: dpb il 9 Lug 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...

Accedi per commentare.


Image Analyst
Image Analyst il 10 Lug 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.

Categorie

Scopri di più su Environment and Settings in Help Center e File Exchange

Prodotti


Release

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by