필터 지우기
필터 지우기

concatenate array of structures with same field names

조회 수: 5 (최근 30일)
gujax
gujax 2023년 6월 5일
댓글: gujax 2023년 6월 6일
Hi
I am reading datafile from a camera which spits out data in a certain format - about several Gb's in size.
The output format is more complex than the example below:
m = memmapfile(def_file,'Format',{'uint8',[8,1],'identifier';'uint8',[504,1],'Header';'uint16',[2^16,1],'Pix'},'Repeat',N,'Offset',0,'writable',false);
where N=8000
and this is the output
m.Data
8000×1 struct array with fields:
identifier
Header
Pix
so that m.Data.Pix has 8000 fields each with 65536 element vector
and m.Data.identifier also has 8000 fields each with 8 element vector
I want to concatenate each structure array example for N=20 is a script I adapted from the forum
M = CatStructFields(m.Data,1,1);
tic
Dat = CatStructFields(m.Data,1,3);
toc
To concatenate N=8000 for Pix (j=3) takes ~ half an hour.
There must be a faster way to do this. Any ideas here?
%%
function M = CatStructFields(S, dim, j)
fields = fieldnames(S);
M=[];
for k = 1:numel(S)
aField = fields{j};
M = cat(dim, M, S(k).(aField));
end
end
  댓글 수: 1
gujax
gujax 2023년 6월 5일
Its way faster (like 7 seconds) if I dump the whole file using memmap as a uniform *uint16 into a Matlab variable and then sort out different fields.
But I ran the same using C++ and fstream where you can read a structure directly (where the struct is {identifier, header, Pix, ...}) and it was 10x faster than the uniform uint16 bit dumped version of my code.
So why is it that Matlab cannot use fread to read a structured file? Not sure how fread is implemented (does it use C++ fread or fstream?)

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

채택된 답변

Matt J
Matt J 2023년 6월 5일
편집: Matt J 2023년 6월 5일
So why is it that Matlab cannot use fread to read a structured file? Not sure how fread is implemented (does it use C++ fread or fstream?)
I think it may have to do with the fact that Matlab does not assume that the field data in a struct array are of a fixed size. Therefore, structs are neither stored nor read in contiguously.
  댓글 수: 9
Matt J
Matt J 2023년 6월 6일
I think it's because m is not of type struct, and so the indexing syntax I was using does not apply. You could probably do,
S=m.Data;
M2=cat(1,S.Pix);
gujax
gujax 2023년 6월 6일
yes that worked! Of course m is a filetype. The 'dot' in m.Data led me off thinking its a structure! Should have paid more attention

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by