How do I use a user input string to reference a structure that already exists?
Afficher commentaires plus anciens
I want the user to input a string and then I want to use that name to reference an existing structure so that I can redefine a field in another existing structure.
Example-
UserInput='ExistingStructure'
ExistingStructure.VariableName.Value=DifferentStructure.VariableName.Value
Is there a simple method for doing this?
4 commentaires
JT
le 20 Sep 2021
Walter Roberson
le 20 Sep 2021
Why not make the interface
ExistingStructure = YourFunction(ExistingStructure)
where the user is responsible for saving the updated result?
"I could ask users to hard code their individual structures but some may not have high levels of matlab ability. It's better if they can just define their structure name."
I doubt that, that sounds like a very fragile, inefficient, buggy way to write code.
A much more robust approach is to let your users simply call a function with whatever input arrays they want.
"Maybe this is impossible?"
Why do you think that? The page that I linked to gives links to many examples of how you could do this.
Réponse acceptée
Plus de réponses (1)
Chunru
le 18 Sep 2021
Here is one solution without dynamically naming a variable:
...
UserInput='ExistingStructure';
if exist(userInput, 'var')
ExistingStructure.VariableName.Value=DifferentStructure.VariableName.Value;
else
disp("The variable " + userInput + "doesn't exist");
end
2 commentaires
JT
le 20 Sep 2021
Stephen23
le 20 Sep 2021
"I don't want to hard code it in. I have multiple users with different structure names that need to use this. I want them to be able to input their structure name into the string and then just have it work."
How do all of these users get these many structures into one workspace at once: twenty people using one PC?
I doubt that you do that... so most likely at some point you have to import the users' data, and that would be the suitable and easy place to handle this better.
"In short, I appreciate your efforts but the dynamic definition is the fundemental goal of this question."
This even has a name:
Catégories
En savoir plus sur Characters and Strings dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!