필터 지우기
필터 지우기

Extracting data fom nested object arrays

조회 수: 1 (최근 30일)
Sigurd Askeland
Sigurd Askeland 2018년 4월 24일
편집: Matt J 2018년 4월 24일
I have object arrays where the objects have properties that them-self are object arrays. What is the best way to extract data from the "bottom layer"?
A single data point can be accessed in this way:
dataIwant(3,2) = topLayer(3).middleLayerProperty(2).bottomLayerProperty;
  1. Is there a one-liner for creating the entire dataIwant?
  2. Why is this so slow?
dataIwant = [topLayer.middleLayerProperty];
dataIwant = [dataIwant.bottomLayerProperty];
It took more than 0.015s for a 600 (top layer) x 4 (middle layer) object array.

채택된 답변

Matt J
Matt J 2018년 4월 24일
Is there a one-liner for creating the entire dataIwant?
Not using out-of-the-box MATLAB features, but you can overload the subsref method of the class to have any behavior that you want.
Why is this so slow?
It depends on many factors, such as the memory consumed by the array, your MATLAB version, whether you executed this from the command line or inside a function, whether you ran this once or many times (so that the commands could cache).
But the statements you've shown are not trivial operations. They build whole new arrays, and so require memory allocation and copying. You should consider whether splitting your middle layer across many objects is really the best data organization for you. It might be better to have a single object with an array of middle-layer objects, so that you can index like this
dataIwant(3,2) = topLayer.middleLayerProperty(3,2).bottomLayerProperty;
  댓글 수: 2
Sigurd Askeland
Sigurd Askeland 2018년 4월 24일

Thanks!

Why is this so slow?

I improved the speed three-fold by making the objects inherit handle. (That may come back to bite me...) That would indicate that memory allocation and copying was the problem. It is still as slow as a for loop, though.

Is there a one-liner ...

I would love for there to be at least this kind of functionality:

    dataIwant(:,2) = [topLayer.middleLayerProperty(2).bottomLayerProperty];

Can that be done by overloading the subsref method?

Matt J
Matt J 2018년 4월 24일
편집: Matt J 2018년 4월 24일

The indexing syntax is totally in your control when you overload subsref. Be mindful, though, that once you do, you will also have to supply definitions of dot-indexing statements, e.g.,

   toplayer.property

to have this syntax outside the classdef file.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by