How can I use a variable for addpath()

9 ビュー (過去 30 日間)
Stefano
Stefano 2017 年 7 月 20 日
編集済み: Jan 2017 年 7 月 21 日
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
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
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
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.

Jan
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 件のコメント
Stefano
Stefano 2017 年 7 月 21 日
ans = 7
C:\Users\stefano\Desktop\test
ans =
char
But it still does not detect the path. I am trying to use the files inside the folder but it gives me this error: Undefined function or variable 'time_sec'. (time_sec is inside the folder)
Jan
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.
Try rehash after appending the new folder.

サインインしてコメントする。


Stefano
Stefano 2017 年 7 月 20 日
Thank you all for the help!! I managed to make it work by changing the strcat for fullfile and converting folder_name to char. The final code looks like this:
if true (ignore)
% code (ignore)
end (ignore)
first_c= 'C:\Users\';
second_c='\Desktop\';
user_name= getenv('username');
folder_name=get(handles.edit1,'String');
folder_name= char(folder_name);
com_path= fullfile(first_c,user_name,second_c, folder_name);
addpath(com_path);
  1 件のコメント
Stephen23
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 ExchangeFile Operations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by