필터 지우기
필터 지우기

How to find the maximum index in a struct

조회 수: 3 (최근 30일)
EK
EK 2021년 4월 23일
댓글: Stephen23 2021년 4월 23일
I need to read in an unknown number of data files from the current working directory. Each data file has a filename that contains the word "metrics".
To do this, I am using the following to read these into a struct:
>> F = dir("*metrics*")
This works. I get the following:
F =
3×1 struct array with fields:
name
folder
date
bytes
isdir
datenum
I happen to know that in this test directory, there are 3 files that match this pattern, and in the above return I can see that F is a 3x1 struct array.
Accessing F(1).name gives the name of the first file, F(2).name gives the name of the second and so on.
The problem I have is that in the live directory (not the test one) there can be an unknown number of files, and I need to process each one of them in turn.
I though that I could use 'size' to get the dimensions of the struct array (as returned in the above), but I get this instead:
>> size F
ans =
1 1
So, the quesiton is, how do I figure out how many filename "entries" there are in this struct F, so that I can use a loop to process each index in turn?
I was planning on doing something like this:
for index = 1:numberOfFiles
myFileProcess(F(index))
end
Please note that I am not asking about how to access individual elements of the entries in the struct (like b = F(2).bytes) - I need to know how many things there are in this struct (i.e filenames returned from dir).
Thanks!
  댓글 수: 1
Stephen23
Stephen23 2021년 4월 23일
Note that
size F
is equivalent to
size('F')
which measures the size of the scalar character 'F' (note that strings are colored purple). Compare:
size(F) % what you should do

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

채택된 답변

Star Strider
Star Strider 2021년 4월 23일
편집: Star Strider 2021년 4월 23일
Try something like this instead:
Fsize = size(F)
The function form is likely to give the result you want.
EDIT — (23 Apr 2021 at 4:30)
If you only want to know how many files there are in ‘F’:
Flen = numel(F)
works.
  댓글 수: 2
EK
EK 2021년 4월 23일
Thanks!
Flen = numel(F)
is exactly what I was looking for.
Appreciate the help.
Star Strider
Star Strider 2021년 4월 23일
As always, my pleasure!

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by