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!