Function handle in structure field generating data from other fields

5 vues (au cours des 30 derniers jours)
Hi everyone,
I have a structure and in one of its fields I want to store some data. It is large data, but generation of it is very fast. Since the structure needs to be saved as matfile, generating the data when needed (i.e. when accessing the struct field) would be much better than storing it with the struct. Is this possible to solve this, maybe with function handles?
Lets assume a function
function[DataOut]=MyFunction(Locs, Peaks)
% some random stuff happening to generate DataOut
end
and a struct with the fields
MyStruct.Locs=[1 3 5 7 9];
MyStruct.Peaks=[40 50 60 70 80];
MyStruct.ProcessedData=@MyFunction
Is is somehow possible to just access the field MyStruct.ProcessedData and to get the data that have been produced by using the fields "MyStruct.Locs" and MyStruct.Peaks" ? That means that I would have to tell the function handle which data to use when making the handle... But how?
I think this is close to something like object oriented programming and a special method invoked when trying to access a field. But I have not much knowledge about this kind of topics. You help is very much appreciated!
Thanks in advance!
  6 commentaires
Maximilian Rüppell
Maximilian Rüppell le 8 Avr 2019
I am trying to describe better then: A structure "MyStruct" holds data, should be as small as possible because being stored, reloaded, worked on, stored again.
Multiple fields of "MyStruct" contain data. One function "MyFunc" takes these data and processes them (for example some baseline substraction or similar). Storing the results in MyStruct as well is possible (and I could easily do so). But the data could also be generated on the fly/ whenever the field is accessed to make MyStruct smaller.
Is it possible to make a field of a struct behave as if the was data stored but instead generating the data at the moment of accessing the field? Someone accessing the struct field should see no difference to a "normal" field and should just be able to retrieve the data.
Maximilian Rüppell
Maximilian Rüppell le 8 Avr 2019
Steven, I am not sure since I did not fully understand. Would I be able to just access that field like any other field and get the processed data as return value, although their only generated that moment?
And would it be possible to integrate this functionality into an already existing struct?

Connectez-vous pour commenter.

Réponse acceptée

Guillaume
Guillaume le 8 Avr 2019
With a structure, it's not possible to do what you want.
With a class, yes, using dependent properties.
classdef DemoClass
properties
Locs;
Peaks;
end
properties (Dependent)
SomethingElse;
end
methods
function value = get.SomethingElse(this)
%code to calculate SomethingElse.
end
end
end
SomethingElse will be calculated every time it is accessed by the user.

Plus de réponses (0)

Produits


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by