Extract numerical values from structure
8 次查看(过去 30 天)
显示 更早的评论
Hi
I'm taking input parameters from a dialog box which MATLAB then stores in a cell array. I this cell to an structure by using the command: 'cell2struct'.
Now if I want to use the numerical values in the different structure fields it gives me a matrix of certain size except the actual value in single quotes.
Can someone please help on how do I extract only the actual value if I want to use it elsewhere in my code.
Below is what I have so far:
prompt = {'Air Temperature [Degrees Celsius]', 'Air Pressure [Pa]', 'Drag Coefficient','Dart Mass [kg]', 'Firing Angle [degrees]', 'Initial Velocity [m/s]', 'Lattitude [degrees]', 'Elevation [m]'};
dlgtitle = 'System Parameters';
dims = [1 50];
definput = {'17','102000','0.9183','0.009487','5','80','22.111620','66'};
answer = inputdlg(prompt,dlgtitle,dims,definput,'on');
fields = {'T','P','Cd','mass','theta','v0','lat','elevation'};
Par = cell2struct(answer, fields,1);
0 个评论
采纳的回答
Star Strider
2019-7-11
One approach to extracting the numerical values from the cell array of strings that inputdlg returns here is to add an assignment that extracts the numbers from the characters and then converts it to a cell array:
danswer = num2cell(str2double(answer));
Par = cell2struct(danswer, fields,1);
producing:
Par =
struct with fields:
T: 17
P: 102000
Cd: 0.9183
mass: 0.009487
theta: 5
v0: 80
lat: 22.112
elevation: 66
No quotes!
The rest of your code is unchanged.
2 个评论
Star Strider
2019-7-11
As always, my pleasure!
I much prefer inputdlg (and its friends), however they need just a bit of help afterwards to recover the data they return.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!