Strange problem while using Matlab structures

1 view (last 30 days)
Hi,
I have a structure named plotLevelLiDARData with 3 columns and 365708 entries for each column. I tried to do the following steps:
1. data = plotLevelLiDARdata;
2. dataCopy = plotLevelLiDARdata;
3. data.x = dataCopy.x(chosenIndices);
4. data.y = dataCopy.y(chosenIndices);
5. data.z = dataCopy.z(chosenIndices);
On executing the last three steps (i.e., 3-5), I find that the values of all the three structure (i.e., plotLevelLiDARData ,data, dataCopy) are affected. Please see the attachment for some debug screenshots. It can be seen that the value X:365708x1 becomes x: 3126x1 as soon as I run the step 3.
The problem might be that Matlab is considering allocating the same memory pointer to the three structures. Could you please tell if there is a problem with my coding?
Thank you! Aravind
  2 Comments
Stephen23
Stephen23 on 11 Jan 2017
Edited: Stephen23 on 11 Jan 2017
@Aravind Harikumar: do not give us screen shots or images of code. Such images are useless. We cannot run images. We cannot search images of code. If you want help with your problem then please give us code. Code is text. We can run code when it is text.
Please edit your question and get rid of the useless images, and instead give us all of the code that we need to be able to replicate your problem. You might want to use rand or randi to create those matrices of data. Run your code. Check that it creates this issue, and then paste it into your question so that we can run it too.
Aravind Harikumar
Aravind Harikumar on 11 Jan 2017
My sincere apologies for vaguely posting the question. I have recreated the issue (as was mentioned before) in the following code attached. The main program is issueRecreation.m. The issue can be observed in the Debug mode after executing line 20. I hope the issue can be reproduced.!

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 11 Jan 2017
Edited: Guillaume on 12 Jan 2017
"I have a structure"
No, you have not. As your screenshot shows (and just pasting the code would have been better, see Stephen's comment), you have an object of class lasdata.
The syntax for accessing object properties and structure fields might be the same, but they are very different things. In particular, the lastdata class is obviously a handle class, hence all copies share the same memory.
If plotLevelLiDARdata was a structure you would have seen this:
>>plotLevelLiDARdata
plotLevelLiDARdata =
struct with fields:
Unless the lasdata class has its own copy method, or it derives from matlab.mixin.Copyable, the only way for you to create a copy is:
data = lasdata; %creates a NEW object. Assumes that |lasdata| has a constructor which works with no arguments
data.x = plotLevelLiDARdata.x; %copy properties
data.y = plotLevelLiDARdata.y;
data.z = plotLevelLiDARdata.z;
edit, now that you've posted the details of the lasdata class:
The above won't work because lasdata constructor needs at least one argument. More importantly, the author of that class did not appear to be fully familiar with access protection but it's clear that he did not intend for the user to be able to change the properties of the class.
If you really need several distinct instances of the objects, you need to create them all from scratch, with no copy:
filepath = fullfile(inFilepath, '000415_elli_85_0002.las'); %fullfile is better than strcat for building paths
originalData = lasdata(filepath);
dataCopy1 = lasdata(filepath);
dataCopy2 = lasdata(filepath);
If the copies of the object don't need to be instances of the class (i.e. you don't intend to use any of the lasdata methods on them), you could also do:
originalData = lasdata(fullfile(inFilepath, '000415_elli_85_0002.las'));
dataCopy1 = struct(originalData);
dataCopy2 = dataCopy1; %dataCopy1 is a struct, not a handle object, so it's fine to just copy with =
  1 Comment
Aravind Harikumar
Aravind Harikumar on 12 Jan 2017
Thank a lot for helping me understand the mistake :) It was really helpful!

Sign in to comment.

More Answers (1)

Philip Borghesani
Philip Borghesani on 11 Jan 2017
Edited: Philip Borghesani on 11 Jan 2017
lasdata in the file exchange is a handle class so plotLevelLiDARdata is not a structure, it is a handle class type object. With handle classes data, dataCopy, and plotLevelLiDARdata will be the same object so yes everything is shared between the variables. I suggest learning about how handle objects work.

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!