XLSWRITE - into same folder location as M file

I want to store my M files on a USB stick for protability. Some of these M files create xlsx files as an output. I would like to store these in the same folder location as the M file which creates them. I used the usual xlswrite ('filename.xlsx', variable); but get an error:
Error using xlswrite (line 220)
Invoke Error, Dispatch Exception:
Source: Microsoft Excel Description: Microsoft Excel cannot access the file 'C:\WINDOWS\system32\38C41000'. There are several possible reasons:
• The file name or path does not exist.
• The file is being used by another program.
• The workbook you are trying to save has the same name as a currently open workbook.
Many thanks.

 Akzeptierte Antwort

Vineet Guru
Vineet Guru am 6 Jun. 2013

0 Stimmen

Thanks guys for your attempts. Your help is most appreciated.
I eventually stumbled upon the "uigetfile" instruction as with just a one click on the m file of interest in a folder, and the uigetfile picks up the folder + filename. I then stripped the filename and retained the folder.

2 Kommentare

Walter Roberson
Walter Roberson am 6 Jun. 2013
If you just want the directory, then uigetdir() would be more appropriate.
Iain
Iain am 6 Jun. 2013
If you know the name of the files, you could use "which" to do it automatically as well.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Azzi Abdelmalek
Azzi Abdelmalek am 6 Jun. 2013

1 Stimme

You should specify the folder when using xlswrite
xlswrite('yourolder/yourfile.xls',data)

4 Kommentare

Vineet Guru
Vineet Guru am 6 Jun. 2013
Hi Azzi, Thanks for your response. I did this previously but I do not want to write this path everytime as these folder paths might change depending upon where the M file moves to. What I am looking for is some global instruction which automatically writes the XLS file to the same M file location, even though I do not know the full path.
Thanks.
Azzi Abdelmalek
Azzi Abdelmalek am 6 Jun. 2013
Bearbeitet: Azzi Abdelmalek am 6 Jun. 2013
Then use pwd to guet the current folder
yourfolder=pwd
xlswrite([ youfrolder '\yourfile.xls'],data)
Vineet Guru
Vineet Guru am 6 Jun. 2013
The pwd instruction points to C:\windows\system32, which is not where my M file is.
Azzi Abdelmalek
Azzi Abdelmalek am 6 Jun. 2013
Bearbeitet: Azzi Abdelmalek am 6 Jun. 2013
How are you running your m-file? your m-files, are not in the current folder? And I am not getting what the aim of what you are looking for

Melden Sie sich an, um zu kommentieren.

Iain
Iain am 6 Jun. 2013
Bearbeitet: Iain am 6 Jun. 2013

1 Stimme

[p f] = fileparts(mfilename('fullpath'));
xlswrite([p '\excel filename.xls'], ...)

3 Kommentare

Vineet Guru
Vineet Guru am 6 Jun. 2013
Hi Iain,
Thanks for your reply.
However, is there a Matlab instruction that automatically grabs the pathlink used in
[p f] = fileparts('pathlink+mfilename');
The pwd instruction I used points me to C:\windows\system32
while my M file is store in:
e:\matlab\ure01\unsupervised.m
I would like to grab this (e:\matlab\ure01\unsupervised.m) automatiically without having to type in the path, which often is unknown or too long and tedious to type in everytime the M file location changes.
Iain
Iain am 6 Jun. 2013
mfilename('fullpath') grabs the path, and the filename of the m file (but not the .m bit)
Jan
Jan am 6 Jun. 2013
Bearbeitet: Image Analyst am 6 Jun. 2013
fullfile(p, 'excel filename.xlsx') considers potentially existing trailing and OS depending file separators automatically.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu App Building finden Sie in Hilfe-Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by