Adding prefix to indexe

9 ビュー (過去 30 日間)
julian gaviria
julian gaviria 2022 年 12 月 5 日
コメント済み: julian gaviria 2022 年 12 月 6 日
Goal: To set the source path for using the "copyfile" function
Question: How can I add a prefix to the 7th subloder? this folder changes across multiple folders containing data from different individuals. A coulple of examples:
/subf1/subf2/subf3/subf4/subf5/subf6/sub-s001/subf8/subf9/
/subf1/subf2/subf3/subf4/subf5/subf6/sub-s002/subf8/subf9/
THe following code did not work:
SourcePath ='/subf1/subf2/subf3/subf4/subf5/subf6/';
index = 1001 : 1002;
%iteration for the 7th subfolder
for i = 1 : length(index)
newParticipant=num2str(index(i));
newParticipant(1)='sub-s'; % error mesagge: Unable to perform assignment because the left and right sides have a different number of elements.
SourceFolder = fullfile([SourcePath,newParticipant],'subf8/subf9');
end
THanks in advance for your hints

採用された回答

VBBV
VBBV 2022 年 12 月 5 日
SourcePath ='/subf1/subf2/subf3/subf4/subf5/subf6/';
index = 1001 : 1002;
%iteration for the 7th subfolder
for i = 1 : length(index)
newParticipant=num2str(index(i));
T = ['sub-s',newParticipant]; % error mesagge: Unable to perform assignment because the left and right sides have a different number of elements.
SourceFolder = fullfile([SourcePath,T],'subf8/subf9')
end
SourceFolder = '/subf1/subf2/subf3/subf4/subf5/subf6/sub-s1001/subf8/subf9'
SourceFolder = '/subf1/subf2/subf3/subf4/subf5/subf6/sub-s1002/subf8/subf9'
  7 件のコメント
julian gaviria
julian gaviria 2022 年 12 月 6 日
It nicely worked. Thank you so much @VBBV

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

その他の回答 (1 件)

Steven Lord
Steven Lord 2022 年 12 月 5 日
This section of code, as originally written, assumes you're on Linux or Mac. We can make this a bit more platform independent.
if ispc
rootDir = 'c:\'; % Assuming this should be relative to the C drive's root
else
rootDir = '/';
end
SourcePath = fullfile(rootDir, 'subf1', 'subf2', 'subf3', 'subf4', 'subf5', 'subf6');
Now let's assemble the participant part.
index = 1001 : 1002;
%iteration for the 7th subfolder
for i = 1 : length(index)
As you saw when you tried, you can't put 'sub-s' (5 characters) into 1 element of newParticipant. I'm going to use a string array here instead of a char array because it makes the code to define newParticipant really easy.
newParticipant = "sub-s" + index(i);
Now let fullfile do all the work for you; don't mix manually concatenating the source path, the participant directory name, and more sub-paths together.
SourceFolder = fullfile(SourcePath,newParticipant,'subf8','subf9')
end
SourceFolder = "/subf1/subf2/subf3/subf4/subf5/subf6/sub-s1001/subf8/subf9"
SourceFolder = "/subf1/subf2/subf3/subf4/subf5/subf6/sub-s1002/subf8/subf9"
Note that because newParticipant was a string array, so are both the SourceFolder variables. Most functions that accept char arrays should also accept string arrays, but if you have to use a function that doesn't call char on it to convert it into a char vector. Note that even though the code above displayed two SourceFolder values, that's because it was in a for loop. The variable contains only the value from the last iteration, so SourceFolderChar will contain only one char vector.
SourceFolderChar = char(SourceFolder)
SourceFolderChar = '/subf1/subf2/subf3/subf4/subf5/subf6/sub-s1002/subf8/subf9'
  1 件のコメント
julian gaviria
julian gaviria 2022 年 12 月 5 日
Thank you so much @VBBV and @Steven Lord
There's an issue with the loop index: it should be "sub-s001" instead of "sub-s1001"
To recall, I have the following source:
/subf1/subf2/subf3/subf4/subf5/subf6/sub-s001/subf8/subf9/
/subf1/subf2/subf3/subf4/subf5/subf6/sub-s002/subf8/subf9/
/subf1/subf2/subf3/subf4/subf5/subf6/sub-s003/subf8/subf9/
if ispc
rootDir = 'c:/'; % Assuming this should be relative to the C drive's root
else
rootDir = '/';
end
SourcePath = fullfile(rootDir, 'subf1', 'subf2', 'subf3', 'subf4', 'subf5', 'subf6');
index = 1001 : 1003;
%iteration for the 7th subfolder
for i = 1 : length(index)
%newParticipant(1)='S';
newParticipant = "sub-s" + index(i);
SourceFolder = fullfile(SourcePath,newParticipant,'subf8','subf9')
SourceFolderChar = char(SourceFolder)
end
Output:
of course I tried out a different index:
index = 001 : 003;
Output:

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

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by