How can I use a variable for addpath()
9 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
Stephen23
2017 年 7 月 20 日
編集済み: Stephen23
2017 年 7 月 20 日
"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
2017 年 7 月 20 日
編集済み: David Goodmanson
2017 年 7 月 20 日
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.
回答 (3 件)
Steven Lord
2017 年 7 月 20 日
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 件のコメント
Jan
2017 年 7 月 20 日
編集済み: Jan
2017 年 7 月 20 日
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 件のコメント
Jan
2017 年 7 月 21 日
編集済み: Jan
2017 年 7 月 21 日
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
2017 年 7 月 20 日
1 件のコメント
Stephen23
2017 年 7 月 20 日
編集済み: Stephen23
2017 年 7 月 20 日
@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.
参考
カテゴリ
Help Center および File Exchange で File Operations についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!