Create a structure of a field in the workspace

조회 수: 24 (최근 30일)
Samy Alkhayat
Samy Alkhayat 2022년 8월 16일
댓글: Samy Alkhayat 2022년 8월 16일
Hello,
I am trying to create a structure of all variables in the workspace that includes a field value of those variables.
For example, I have variables A, B, C, and I want to have:
A.Value = A;
B.Value= B;
C.Value= C;
I did this for all 200 variables in myworkspace and I am getting the error below:
Unable to perform assignment because dot indexing is not supported for variables of this type.
Can you please advice if there's more efficient way? Thanks in advance
  댓글 수: 5
Stephen23
Stephen23 2022년 8월 16일
"I am trying to create a structure of all variables in the workspace that includes a field value of those variables."
If you want one structure, why are you creating three separate structures in your example?
Samy Alkhayat
Samy Alkhayat 2022년 8월 16일
@Stephen23 the reason is to align with a Simulink model that all parameters are structured like in " A.Value "
So I need to use a structure of each field in my workspace to be .Value

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

채택된 답변

Dyuman Joshi
Dyuman Joshi 2022년 8월 16일
편집: Dyuman Joshi 2022년 8월 16일
You are overwriting numeric values (double precision) with struct values. That is not allowed and hence the error.
If all your variables are individually defined, then you will have to assign them manually. If they were in a matrix, we could have used for loop.
Assigning them to a single structure -
A=3;B=4;C=5;
str(1).Value=A;
str(2).Value=B;
str(3).Value=C;
...
val=[str(1:3).Value]
val = 1×3
3 4 5

추가 답변 (1개)

KSSV
KSSV 2022년 8월 16일
You can save them into a matfile and access, as you like.
A = 1 ;
B = 2;
C = 3 ;
save Test.mat ;
S = matfile('Test.mat') ;
S.A
S.B
S.C

카테고리

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

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by