On MATLab app designer, how can i get an editfield text as a variable?
19 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello, I have am developing a matlab app, where the app user inputs a function to an edit field, and i need to use whatever that function is in a later part of the code. The problem is that the editfield outputs as text rather than a variable/function. Does anyone know how can I output it as a variable?
The code is as follows:
So say user inputs to EditField: x(1)^3 + x(2)^2
Then I want to set: val = app.EditField.Value;
However, this gives me the text: val = 'x(1)^3 + x(2)^2'
When i want: val = x(1)^3 + x(2)^2
Is this possible? Thanks for the help.
2 commentaires
Fangjun Jiang
le 9 Juil 2020
Do you mean you have x defined,e.g. x=1:3, you want to Val=1^3+2^2=5?
or do you want val=f(x)=x(1)^3 + x(2)^2 as a function handle?
Réponses (2)
Fangjun Jiang
le 9 Juil 2020
EditFieldText='x(1)^3 + x(2)^2';
F=str2func(['@(x)',EditFieldText]);
Then pass the function handle F to another function and use it. You don't have to worry about variable name x.
>> y=1:3
y =
1 2 3
>> F(y)
ans =
5
or you can pass EditFieldText over to the other function and construct the function handle there
0 commentaires
Arthur Roué
le 9 Juil 2020
2 commentaires
Dennis
le 9 Juil 2020
It might be worth to mention that this might cause problems down the road depending on your apps users. eval will evaluate any expression entered - including for example system commands.
Voir également
Catégories
En savoir plus sur Characters and Strings dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!