How can I use a variable for addpath()
9 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I am trying to create a path for the user in my GUI and I can`t get it to work because the addpath() funktion does not accept the input. This is the code:
first_string= 'C:\Users\';
second_string='\Desktop\';
user_name= getenv('username');
folder_name=get(handles.edit1,'String');
com_path= strcat(first_string,user_name,second_string, folder_name);
addpath(com_path);
Has anyone an idea how can I make the function to recognize the path so that it works?
2 commentaires
Stephen23
le 20 Juil 2017
Modifié(e) : Stephen23
le 20 Juil 2017
"I can`t get it to work because the addpath() funktion does not accept the input"
How you do you know that it does not work? What error or warning message do you get? Please show us what this displays:
double(com_path)
Note that you should definitely use fullfile rather than awkward and buggy strcat with file separators.
David Goodmanson
le 20 Juil 2017
Modifié(e) : David Goodmanson
le 20 Juil 2017
Hi Sefano, Strings and character arrays aren't the same. I don't know about addpath and I don't want to mess around with my path commands to find out, but lots of commands won't take strings. For example, which('sin') works, which(string('sin')) doesn't.
You have the string option in the 'get' command. Just for info:
>> strcat('a','b')
ans = 'ab' % char array, not a string.
% this is documented, but it seems like a misnomer.
>> strcat('a',string('b'))
ans = "ab" % string
Note that the char command will convert a string to a character array.
Réponses (3)
Steven Lord
le 20 Juil 2017
I doubt this is a string versus char issue. I suspect this is a char versus cellstr issue.
>> P = pwd;
>> C = {P};
>> whos P C
Name Size Bytes Class Attributes
C 1x1 118 cell
P 1x3 6 char
>> addpath(P)
>> addpath(C)
Error using catdirs (line 25)
All arguments must be character vectors.
Error in addpath (line 63)
p = catdirs(mfilename, varargin{1:n});
My guess is that folder_name is a cell array containing char arrays. The String property of a uicontrol of Style 'edit' can be either a character vector or a cell array of character vectors. strcat will combine a char vector and a cellstr and the result will be a cellstr, but addpath does not accept a cell array even if it is a cellstr.
0 commentaires
Jan
le 20 Juil 2017
Modifié(e) : Jan
le 20 Juil 2017
Try:
folder_name = char(get(handles.edit1,'String'));
com_path = fullfile(first_string, user_name, second_string, folder_name);
addpath(com_path);
exist(com_path, 'file')
disp(com_path)
class(com_path)
What do you observe?
2 commentaires
Jan
le 21 Juil 2017
Modifié(e) : Jan
le 21 Juil 2017
Ah, now the problem starts to get clear: addpath works correctly, when the folder is added to the path. But this is not your problem, but Matlab does not find the new files immediately. Correct? The description "addpath() funktion does not accept the input" was misleading.
Stefano
le 20 Juil 2017
1 commentaire
Stephen23
le 20 Juil 2017
Modifié(e) : Stephen23
le 20 Juil 2017
@Stefano: the whole point of using fullfile is that you do not need to write all of those hard-coded filepath separators. Get rid of them.
Also you should accept the answer that helped you most, and vote for any that were useful to you: that is how you can best say thank you to the volunteers who gave their time to help you.
Voir également
Catégories
En savoir plus sur File Operations dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!