Extract numerical values from structure
7 ビュー (過去 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!