How do I combine strings using strjoin without spaces being introduced?
179 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Justin Skaggs
le 9 Mar 2020
Commenté : Justin Skaggs
le 9 Mar 2020
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!
0 commentaires
Réponse acceptée
Akira Agata
le 9 Mar 2020
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.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Characters and Strings 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!