필터 지우기
필터 지우기

Rename folders using folder names saved in cells

조회 수: 2 (최근 30일)
Alberto Acri
Alberto Acri 2023년 6월 18일
댓글: Steven Lord 2023년 6월 19일
I have a folder 'A' inside which I have numbered folders. I saved the folder numbers inside a cell:
cell_numbers = {2;10;22;48};
I want to rename those folders with the names contained in this cell:
cell = {'parameter_1';'parameter_2';'parameter_3';'parameter_4'};
So I want that:
- the folder '2' will be called 'parameter_1'
- the folder '10' will be called 'parameter_2'
- the folder '22' will be called 'parameter_3'
- the folder '48' will be called 'parameter_4'

채택된 답변

the cyclist
the cyclist 2023년 6월 18일
Here is one way:
cell_numbers = {2;10;22;48};
cell = {'parameter_1';'parameter_2';'parameter_3';'parameter_4'}; % <---- You should rename this variable, because "cell" is a MATLAB function name
for nc = 1:numel(cell_numbers)
movefile(sprintf("%d",cell_numbers{nc}),cell{nc})
end
  댓글 수: 11
Alberto Acri
Alberto Acri 2023년 6월 19일
Thank you @the cyclist, it works. It was easier than expected!
Steven Lord
Steven Lord 2023년 6월 19일
You've got a mismatch somewhere between numbers and characters. What are characters 52 and 53?
char(52)
ans = '4'
char(53)
ans = '5'
What are the names of your first two files?
Looking at the third file, characters 49 and 54 are '1' and '6'.
char([49 54])
ans = '16'
I'm guessing somewhere you tried assigning text data into a numeric array using subscripted indexing.
x = 1:10 % numeric array
x = 1×10
1 2 3 4 5 6 7 8 9 10
x(5) = '5' % x(5) becomes 53 because char(53) is '5'
x = 1×10
1 2 3 4 53 6 7 8 9 10
I recommend using a string array instead, as assigning numbers into a string array or vice versa converts them the way you probably expect (5 becomes "5" and vice versa rather than '5' becoming 53 and vice versa.)
x(8) = "64" % Assign string into numeric array
x = 1×10
1 2 3 4 53 6 7 64 9 10
s = string(x)
s = 1×10 string array
"1" "2" "3" "4" "53" "6" "7" "64" "9" "10"
s(2) = 999 % Assign number into string array
s = 1×10 string array
"1" "999" "3" "4" "53" "6" "7" "64" "9" "10"

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by