MATLAB Answers

How do I use a user input string to reference a structure that already exists?

6 views (last 30 days)
JT
JT on 17 Sep 2021
Commented: JT on 21 Sep 2021 at 17:00
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 Comments
Stephen
Stephen on 20 Sep 2021
"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.

Sign in to comment.

Accepted Answer

Steven Lord
Steven Lord on 20 Sep 2021
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.
Rather than trusting your users not to enter an invalid structure name that is instead malicious code (the Bobby Tables problem) I would write a function for your users to call. They can call your function with a struct with whatever name they want, but inside your function you refer to that struct with a fixed name.
myLongStructName = struct('x', 1, 'y', 2); % User's choice of name
z = myfun(myLongStructName)
z = 5
% The q defined out here doesn't interfere with the q in myfun
q = struct('x', 3, 'y', 4);
answer = myfun(q)
answer = 19
function q = myfun(s) % Your choice of name
q = s.x + s.y.^2;
end

More Answers (1)

Chunru
Chunru on 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 Comments
Stephen
Stephen on 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:

Sign in to comment.

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by