String values change when leaving method

1 view (last 30 days)
I am unable to assign string values from one vector to another without what I believe to be pointer errors occuring.
The process:
I assign strings to a string vector:
fileName(k) = string(Filelist(k).name);
I then assign strings from within the string vector to parameters:
obj.channel(i).fileName = fileName(i);
The assignment works as expected, and the strings are all assigned correctly. Upon leaving the method, I print out the values of the strings to console and they a re all completely incorrect! The strings are all the same value and are assigned to the final string value of the string vector.
Instead of the string being:
A B C D E
The string is:
E E E E E
I have a suspicion this has to do with how Matlab assigns pointers and performs garbage cleanup when interacting with multiple scopes/classes. If anybody has a solution to this problem I would greatley appreciate it.
  4 Comments
Ethan Goldstein
Ethan Goldstein on 11 May 2020
Hi Steven, I am wondering if you have had a chance to look into this issue, as I am still unable to figure it out.
Ameer Hamza
Ameer Hamza on 12 May 2020
Ethan, the line of codes in your question is different as compared to your comment. You are using custom get and set methods. Is obj.channel(i) an instance of your custom class? Can you show the definition of these get and set method? Preferably, can you share a small standalone example, with simple class definitions so that we can recreate the error?

Sign in to comment.

Accepted Answer

Ethan Goldstein
Ethan Goldstein on 2 Jul 2020
The problem ended up being I had only allocated one memory location for the string, but pointed at it with multiple instances of different objects. I did not realize I had setup my code to do. This is why I had this problem.

More Answers (0)

Categories

Find more on App Building in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!