Use of pointers to structures
41 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Dear all,
I would like to know if it is possible to use pointers to structures. The case I would like to use this time is like this:
% case_tal,case_cual are structs with data from different systems, I want to loop on them
for i_case = [case_tal,case_cual]
% i_case.file is an array of files that I want to loop within each case
for i_file = i_case.file
i_file.data = do_stuff_on_the_data;
end
end
I want to use pointers because I would like that when I do
i_file.data = do_stuff_on_the_data;
the struct really is modified and store the data, not lose it.
Is it possible?
Thanks in advance!
0 commentaires
Réponses (2)
Adam
le 9 Déc 2014
Modifié(e) : Adam
le 9 Déc 2014
Matlab does not have the concept of pointers.
You can use a class derived from handle to achieve this 'by reference' behaviour instead of a struct, but a struct in Matlab is just a simple object to hang data off and you have to reassign to the struct if you want to change the data.
You can just do:
i_file.data = do_stuff_on_the_data( i_file.data );
though. It's ugly as a programming construct, but it does the job when you can't pass things by reference (or pointer).
Document for reference on classes if you aren't aware of it:
0 commentaires
Matt J
le 9 Déc 2014
Modifié(e) : Matt J
le 9 Déc 2014
The simplest solution would be to simply return i_file as an output from whatever function is performing the code you show. However, to have the semantics you mention, you could create a handle class
classdef myclass<handle
properties
data
end
end
Now, when you create the variable as follows
i_file=myclass;
it will have the behavior you're looking for.
0 commentaires
Voir également
Catégories
En savoir plus sur Structures dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!