Use of pointers to structures

조회 수: 41 (최근 30일)
Ricardo
Ricardo 2014년 12월 9일
편집: Matt J 2014년 12월 9일
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!

답변 (2개)

Adam
Adam 2014년 12월 9일
편집: Adam 2014년 12월 9일
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:

Matt J
Matt J 2014년 12월 9일
편집: Matt J 2014년 12월 9일
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.

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by