Saving current progam folder path

1 visualizzazione (ultimi 30 giorni)
Jason
Jason il 27 Mar 2014
Commentato: Jason il 27 Mar 2014
Hi. I want to save the location of the current m file folder.
%Save current app directory to a txt file
prog = mfilename('fullpath'); %Get current program path & name
[progpath,name,ext] = fileparts(prog); %Split out folderpath
pathtext = fullfile(progpath,'lastdir.txt') %Build new filename
save('pathtext','progpath','-ascii');
But using the above results in the text file (lastdir.txt) containing only numbers?

Risposta accettata

Jacob Halbrooks
Jacob Halbrooks il 27 Mar 2014
Modificato: Jacob Halbrooks il 27 Mar 2014
It looks like you want to write a string to the text file, but SAVE is not a good fit for this. The help for SAVE -ASCII explains:
* MATLAB translates characters to their corresponding internal
ASCII codes. For example, 'abc' appears in an ASCII file as:
9.7000000e+001 9.8000000e+001 9.9000000e+001
I would suggest you use a different function for writing the file, such as FPRINTF:
prog = mfilename('fullpath'); %Get current program path & name
[progpath,name,ext] = fileparts(prog); %Split out folderpath
pathtext = fullfile(progpath,'lastdir.txt') %Build new filename
fid = fopen(pathtext, 'w');
fprintf(fid, '%s', progpath);
fclose(fid);

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by