Extract numerical values from structure
3 views (last 30 days)
Show older comments
Christo van Rensburg
on 11 Jul 2019
Commented: Star Strider
on 11 Jul 2019
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 Comments
Accepted Answer
Star Strider
on 11 Jul 2019
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 Comments
Star Strider
on 11 Jul 2019
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.
More Answers (0)
See Also
Categories
Find more on Structures in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!