Filter löschen
Filter löschen

String values change when leaving method

1 Ansicht (letzte 30 Tage)
Ethan Goldstein
Ethan Goldstein am 7 Mai 2020
Beantwortet: Ethan Goldstein am 2 Jul. 2020
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 Kommentare
Ethan Goldstein
Ethan Goldstein am 11 Mai 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 am 12 Mai 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?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Ethan Goldstein
Ethan Goldstein am 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.

Weitere Antworten (0)

Kategorien

Mehr zu App Building finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by