using the data from struct

조회 수: 4 (최근 30일)
Mohamed Ibrahim
Mohamed Ibrahim 2020년 2월 14일
댓글: Fangjun Jiang 2020년 2월 14일
hello i have a small problem
i have a code and when i run it its solution appear as a struct
the problem is i want to take some data from this struct aىd use it as a input for another code but i can't when i enter the name of a field in structure for example CL matlab tells me Undefined function or variable 'CL'.
i want this structure outputs to appear in my workspace like that
any suggestions please :)
  댓글 수: 2
Fangjun Jiang
Fangjun Jiang 2020년 2월 14일
out of breath reading your question. add proper punctuation marks.
Stephen23
Stephen23 2020년 2월 14일
편집: Stephen23 2020년 2월 14일
You should allocate the structure to a variable, e..g.:
S = function_that_returns_a_structure(...);
Then you can trivially refer to its fields, e.g.:
S.Re
S.CD
S.Ncrit
etc.
"i want this structure outputs to appear in my workspace like that"
Do not convert structure fields to variables, unless you want to force yourself into writing slow, complex, buggy code that is hard to debug.

댓글을 달려면 로그인하십시오.

채택된 답변

Fangjun Jiang
Fangjun Jiang 2020년 2월 14일
If you can't change the other program, Out=YourOtherProgram(), where "Out" is a data like "ans" in your picture,
then you can use Out.CL, Out.CD, etc.
There are functions like struct2array(), struct2cell() that you can use.
  댓글 수: 2
Mohamed Ibrahim
Mohamed Ibrahim 2020년 2월 14일
thanks for your replay :)
i write this line at the end of my code
cth=ans.CL;
and matlab gives me this error message
Attempt to reference field of non-structure array.
Error in xfoil (line 239)
cth=ans.CL;
Fangjun Jiang
Fangjun Jiang 2020년 2월 14일
"ans" is the reserved variable name used by MATLAB. Use a different name.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by