Extract numerical values from structure

12 visualizaciones (últimos 30 días)
Christo van Rensburg
Christo van Rensburg el 11 de Jul. de 2019
Comentada: Star Strider el 11 de Jul. de 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);

Respuesta aceptada

Star Strider
Star Strider el 11 de Jul. de 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 comentarios
Christo van Rensburg
Christo van Rensburg el 11 de Jul. de 2019
Hi Star Strider
It works! Thanks so much, your approach makes sense.
Star Strider
Star Strider el 11 de Jul. de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Structures en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by