Creating a Loop for my work

조회 수: 1 (최근 30일)
Theophane Piette
Theophane Piette 2020년 1월 31일
댓글: Theophane Piette 2020년 2월 4일
Hi everyone,
I've got a old database, with folder with weird name that i have to rename to work on.
All my subject are named EC01 to EC50, and i've creating a code to change what i need to change on EC01:
"mkdir C:\Users\beber\Documents\EC01\RTC f
movefile C:\Users\beber\Documents\EC01\RTC\RTC_0034\graph* C:\Users\beber\Documents\RTC f
rmdir C:\Users\beber\Documents\EC01\RTC\RTC_0034"
And being new on Matlab, i wonder if there is a way to "loop" this code, to ask matlab to this manipulation on all the subject from EC01 to EC50 ?
Thank you for your help :D.

채택된 답변

Jakob B. Nielsen
Jakob B. Nielsen 2020년 1월 31일
편집: Jakob B. Nielsen 2020년 1월 31일
There is indeed! A basic loop runs through integer numbers that you specify. You can then convert these numbers to strings - which is matlab language for text.
Example:
for i=1:50 %whatever you write inside the for loop will execute 50 times, and each time the value of i will increase by 1
numberstring=num2str(i); %now instead of a number e.g. 1, you have a string, '1'.
if i<10 %see below for why we do this!
mydir=['C:\Users\beber\Documents\EC0',numberstring,'\RTC f'];
%the first time the loop runs, will be 'C:\Users\beber\Documents\EC01\RTC f'
%the second time the loop runs, 'C:\Users\beber\Documents\EC02\RTC f'
%the tricky part is, that obviously when you reach 10 you will get out 'C:\Users\beber\Documents\EC010\RTC f'
%we get aroundn that by using "if" statements.
mkdir mydir;
%from there, make the rest of your code, and add more strings as needed. I wont make it all, play around with it a little bit - thats the best way to learn :)
else
filename=['C:\Users\beber\Documents\EC',numberstring,'\RTC f'];
%small but subtle difference. Once i gets to 10, you will no longer put in the 0 before the number.
%so instead of getting EC010, you simply get EC10.
mkdir mydir;
end
  댓글 수: 1
Theophane Piette
Theophane Piette 2020년 2월 4일
Thank you so much that helped me a lot :D

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by