How do I combine strings using strjoin without spaces being introduced?

56 ビュー (過去 30 日間)
Justin Skaggs
Justin Skaggs 2020 年 3 月 9 日
コメント済み: Justin Skaggs 2020 年 3 月 9 日
Hi,
I am currently trying to run a code that goes through several folders of data. I am having a problem with the function strjoin.
I create a string array named conds, and I want to combine it with another string to get a directory. How can I get rid of this space? I do not want to use strrep to remove the spaces, because later on in my code I have a lot of spaces that I don't want deleted.
conditions = ["C1","C2","G1","G2","I-1","I-2","II-1","II-2","III-1","III-2","N1","N2","P1","P2"];
area = '02_Mapping_Small_Area';
for k = 1:length(conditions)
device = conditions(k);
Home = strjoin(['C:\Users\Me\Desktop\03_Figures\',device]) %Save location
Start=['C:\Users\me\Desktop\',area] %Data retrieval location
% Home = strrep(Home,' ','');
end
>>> Home = "C:\Users\me\Desktop\03_Figures\ C1"
>>> Start = 'C:\Users\me\Desktop\02_Mapping_Small_Area'
The space before C1 that occurs in Home is what I want to get rid of. I guess it has something to do with single and double quotations.
Thanks!

採用された回答

Akira Agata
Akira Agata 2020 年 3 月 9 日
By using the second input argument of strjoin, you can control a delimiter. So the following code can concatenate strings without space.
conditions = ["C1","C2","G1","G2","I-1","I-2","II-1","II-2","III-1","III-2","N1","N2","P1","P2"];
area = "02_Mapping_Small_Area";
for k = 1:length(conditions)
device = conditions(k);
Home = strjoin(["C:\Users\Me\Desktop\03_Figures\",device],'');
Start= strjoin(["C:\Users\me\Desktop\",area],'');
% Do something
end
But, for your purpose, I believe it would be better to use fullfile and/or dir functions to create folder path, rather than concatenating a strings.
  1 件のコメント
Justin Skaggs
Justin Skaggs 2020 年 3 月 9 日
Thanks so much!
Yes, fullfile or dir might be better. Thanks for your input!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by