1function [string, file_name] =
user_string(string_name,
string)
2%USER_STRING Get/set a user specific
string
9% Function to get and set a
string in a system or user specific file. This
10% enables,
for example, system specific paths to binaries to be saved.
12% The specified
string will be saved in a file named <string_name>.txt,
13% either in a subfolder named .ignore under
this file
's folder, or in the
14% user's prefdir folder (in
case this file
's folder is non-writable).
17% string_name - String containing the name of the string required, which
18% sets the filename storing the string: <string_name>.txt
19% new_string - The new string to be saved in the <string_name>.txt file
22% string - The currently saved string. Default: ''
23% isSaved - Boolean indicating whether the save was succesful
24% file_name - path of the .txt file used to contain the requested string
26% Copyright (C) Oliver Woodford 2011-2014, Yair Altman 2015-
28% This method of saving paths avoids changing .m files which might be in a
29% version control system. Instead it saves the user dependent paths in
30% separate files with a .txt extension, which need not be checked in to
31% the version control system. Thank you to Jonas Dorn for suggesting this
34% 10/01/2013 - Access files in text, not binary mode, as latter can cause
35% errors. Thanks to Christian for pointing this out.
36% 29/05/2015 - Save file in prefdir if current folder is non-writable (issue #74)
37% 09/01/2018 - Fix issue #232: if the string looks like a file/folder path, ensure it actually exists
38% 15/01/2020 - Added file_name output argument
40 if ~ischar(string_name)
41 error('string_name must be a
string.
');
43 % Create the full filename
44 fname = [string_name '.txt
'];
45 dname = fullfile(fileparts(mfilename('fullpath
')), '.ignore
');
46 file_name = fullfile(dname, fname);
47 default_file_name = file_name;
51 error('new_string must be a
string.
');
53 % Make sure the save directory exists
54 %dname = fileparts(file_name);
55 if ~exist(dname, 'dir
')
56 % Create the directory
68 fileattrib(dname, '+h
');
73 fid = fopen(file_name, 'wt
');
75 % file cannot be created/updated - use prefdir if file does not already exist
76 % (if file exists but is simply not writable, don't create a duplicate in prefdir)
77 if ~exist(file_name,
'file')
78 file_name = fullfile(prefdir, fname);
79 fid = fopen(file_name, 'wt');
83 file_name = default_file_name;
88 fprintf(fid, '%s',
string);
98 fid = fopen(file_name, 'rt');
100 % file cannot be read, try to read the file in prefdir
101 file_name = fullfile(prefdir, fname);
102 fid = fopen(file_name, 'rt');
105 file_name = default_file_name;
112 % Fix issue
#232: if the string looks like a file/folder path, ensure it actually exists
113 if ~isempty(
string) && any(
string==
'\' |
string==
'/') && ~exist(
string) %#ok<EXIST>
function user_string(in string_name, in string)