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

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!

 채택된 답변

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

Thanks so much!
Yes, fullfile or dir might be better. Thanks for your input!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

질문:

2020년 3월 9일

댓글:

2020년 3월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by