필터 지우기
필터 지우기

How to create character arrays via a loop?

조회 수: 16 (최근 30일)
maruljay
maruljay 2020년 8월 23일
댓글: maruljay 2020년 8월 23일
I have a list of files in a folder that I access through the "dir" command. Then I use:
gg=cellstr(char({files(1:end).name}));
for i = 1:length(files)
name{i}=gg{i}(1:13);
end
This gives me a cell array with each of the file names of datasets as shown below:
I want to append the word "Sensor 1, Sensor 2, Sensor 3,...,Sensor 38" to each of these file names. So, that I get a cell array that looks like this:
2012-01-30 00 Sensor 1
2012-01-30 00 Sensor 2
.........
2012-01-30 01 Sensor 1
........
2012-01-30 23 Sensor 1
and so on.
How do i do this?

채택된 답변

Walter Roberson
Walter Roberson 2020년 8월 23일
cellstr(reshape((string(name) + " Sensor " + (1:38)).', [], 1))
  댓글 수: 1
maruljay
maruljay 2020년 8월 23일
Worked like a charm. Thanks a bunch!!!

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

추가 답변 (1개)

Image Analyst
Image Analyst 2020년 8월 23일
Try this:
d = dir('*.m');
fileNames = {d.name}
name = cell(length(fileNames), 1);
name2 = cell(length(fileNames), 1);
for k = 1 : length(fileNames)
thisFileName = fileNames{k};
lastIndex = min([13, length(thisFileName)]);
name{k} = thisFileName(1:lastIndex);
% Make a new array with Sensor k appended.
% If you wanted to, you could overwrite the name cell array instead.
name2{k} = sprintf('%s Sensor %d', name{k}, k)
end

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by