Looping for cell arrays

조회 수: 2 (최근 30일)
Davindra Usov
Davindra Usov 2023년 4월 25일
댓글: Stephen23 2023년 5월 1일
I have a 3x11 cell array called down_files where each entry in the array is a filename and each of the rows corresponds to a different value for an angle. For example, row 1 corresponds to angle 1, row 2 to angle 2 and so on. The code below worked when the cell array was 1x11, but now that I have added two more rows to it I need to work for 3x11.
lengths_arr contains values read from each file. So lengths_arr should end up being a 3x11 matrix with each row corresponding to the correct angles again (row 1 for angle 2, row 2 for angle 2.......)
for i = 1:length(down_files)
file_data = fileread(down_files{i})
f = regexp(file_data, 'length(\d+)?)', 'tokens') % ignore. this is just getting the length values from the files
if ~isempty(f)
L = str2double(f{1})
lengths_arr(end + 1) = L
end
end
many thanks
  댓글 수: 1
Stephen23
Stephen23 2023년 5월 1일
Note that by using a look-around you can avoid one layer of the output's nested cell arrays:
f = regexp(file_data, '(?<=length)\d+)', 'match')

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

답변 (1개)

chicken vector
chicken vector 2023년 4월 25일
Add a second loop for the rows:
for row = 1 : size(down_files,1)
for col = 1 : size(down_files,2)
fileread(down_files{row,col});
end
end
  댓글 수: 2
Davindra Usov
Davindra Usov 2023년 4월 25일
Thank you. This gives me a 1x11 instead of a 3x11 for lengths_arr
Adam Danz
Adam Danz 2023년 5월 1일
lengths_arr(row, col) = L

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

카테고리

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

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by