String values change when leaving method

1 次查看(过去 30 天)
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 个评论
Ethan Goldstein
Ethan Goldstein 2020-5-11
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 2020-5-12
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?

请先登录,再进行评论。

采纳的回答

Ethan Goldstein
Ethan Goldstein 2020-7-2
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 App Building 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by