필터 지우기
필터 지우기

Looping over paths, more than one loop

조회 수: 5 (최근 30일)
Joanna Przeworska
Joanna Przeworska 2021년 3월 1일
답변: Joanna Przeworska 2021년 3월 2일
Dear all,
suppose I have a vector of names:
vectorOfNames = {'A','B','C'};
and my goal is to iterate through this vector
for s = 1:length(vectorOfNames)
% code of variable 1
path1 = strcat('AAAAAAAA/',char(vectorOfNames(s)),'/AAAAAAA');
keepCode(1,s) = {path1};
% code of variable 2
path2 = strcat('BBBBBBBBbbbb/',char(vectorOfNames(s)),'/bbbbbbbbb');
keepCode(2,s) = {path2};
% code of variable 3
path3 = strcat('ccccccc/',char(vectorOfNames(s)));
keepCode(3,s) = {path3};
end
and use each of name to get the paths below, which are stored in an array:
My question: is there any possibility to modify this code to iterate over 2 loops, the first loop over the names (as is) and the second loop over the paths, given that the names are part of the paths and are variable. In this example, there are only 3 types of paths, but in fact I have 35 of them, and doing it the way I showed just makes the code too extensive.
Kind regards,
Joanna Przeworska
  댓글 수: 1
Bob Thompson
Bob Thompson 2021년 3월 1일
I'm sure what you're asking about is possible, but I also don't entirely understand. How would looping over the path change from what you have now? As I see it your loop generates a matrix of file locations for all possible file and folder locations. Are you trying to reduce the number of possible results, or something else? If you are trying to reduce the number of results, how are you correlating the files and paths?

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

채택된 답변

Jan
Jan 2021년 3월 2일
편집: Jan 2021년 3월 2일
I do not understand, what your inputs are exactly. But maybe this idea helps you:
keepCode = cell(length(vectorOfNames), length(whatEver)); % Pre-allocation
for s1 = 1:length(vectorOfNames)
for s2 = 1:length(whatEver)
folder = strcat(wharEver{s2}, vectorOfNames{s1}, '/AAAAAAA');
keepCode{s1, s2} = folder;
end
end
Notes: cellArray{k} is faster than char(cellArray(k)).

추가 답변 (2개)

Joanna Przeworska
Joanna Przeworska 2021년 3월 2일
Dear Bob,
Thank you for your response.
Basically, I want to get the same result as presented, but I would like to store the paths in a separate variable (including the variable part of that path) like below (most likely variable 'paths' is not properly defined, but it serves to illustrate my view), and iterate over that variable, instead of writing each path separately in the code as I presented.
paths =
strcat('AAAAAAAA/',char(vectorOfNames(s)),'/AAAAAAA')
strcat('BBBBBBBBbbbb/',char(vectorOfNames(s)),'/bbbbbbbbb')
strcat('ccccccc/',char(vectorOfNames(s)))
To be more specific, I imagine the code would act as follows. In the 1st loop (over paths), in each iteration, the code reads the path from the variable (e.g. paths), and then executes the 2nd loop (over names), and finally save the entire path.
I'm not sure if something like this can be programmed, but I would like the code to work as described.

Joanna Przeworska
Joanna Przeworska 2021년 3월 2일
Dear Jan,
This idea helps me a lot and solves all my problems related to this exercise.
Thank you very much for your assistance.
Kind regards,
Joanna Przeworska

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by