Assign values to a structure using App designer

Hi, I want to ask a simple question that bothered me a lot.
When I assign values to a struct, like below
BC.theta_res = app.theta_resEditField_3.Value;
BC.theta_sat = app.theta_satEditField_3.Value;
BC.K_sat = app.K_satEditField_3.Value;
BC.lambda = app.lambdaEditField.Value;
BC.h_s = app.h_sEditField.Value;
why they store values as characters not numbers? Each item has a single quote mark on it.
QQ截图20190416221741.png
My input in the app is:
QQ截图20190416221806.png
How to make it to a number?
thanks a lot!

回答(2 个)

There are two different types of edit fields in appdesigner (numeric and text). In the picture you show are only text edit fields (those are usually left aligned, while numeric fields are right aligned). The value of text edit fields is a string and should be converted to a number before doing calculations with it.
fig=uifigure;
f(1)=uieditfield(fig,'position',[100 50 80 40],'value','1');
f(2)=uieditfield(fig,'numeric','position',[100 90 80 40],'value',2);
s.theta=f(1).Value;
s.lambda=f(2).Value;
f(1).Value+5 %does not throw an error!
str2double(f(1).Value)+5

4 个评论

Thank you for your reply! Yes, there are two types of edit fields, what I'm using is Numeric, I don't know why some results are number while some are string. For example:
height = app.HeightmEditField.Value;
slope = app.SlopeangleEditField.Value;
QQ截图20190417104219.png
QQ截图20190417104316.png
'height' and 'slope' are assigned as number, the syntax is the same I think.
I am pretty sure that the edit fields in your previous screenshot are not numeric.
What do you get as result when you run this:
BC.theta_res = app.theta_resEditField_3.Type;
BC.theta_sat = app.theta_satEditField_3.Type;
BC.K_sat = app.K_satEditField_3.Type;
BC.lambda = app.lambdaEditField.Type;
BC.h_s = app.h_sEditField.Type;
I deleted those edit fields and reset as below:
BC.K_sat = app.K_satEditField.Value;
BC.lambda = app.lambdaEditField.Value;
BC.hs = app.hsEditField.Value;
BC.theta_res = app.theta_resEditField.Value;
BC.theta_sat = app.theta_satEditField.Value;
and this time it works:
QQ截图20190418105519.png
QQ截图20190418105608.png
I feel confused about what the difference? why the previous one didn't work.
Hi Dennis,
Thank you very much for your attention, I retyped all fields and now they are runing fine!

请先登录,再进行评论。

Simply just use str2double function:
BC.theta_res = str2double(app.theta_resEditField_3.Value);

类别

帮助中心File Exchange 中查找有关 App Building 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by