필터 지우기
필터 지우기

assigning file from the struct

조회 수: 5 (최근 30일)
Turbulence Analysis
Turbulence Analysis 2022년 12월 14일
편집: Stephen23 2022년 12월 14일
Hi,
I am looking for a some help in reading the file from teh sturct.
Fo example, I have sturct files named 'A' , 'B' , 'C' ,'D' etc. and inside each struct file I have many arrays stored (e.g. X, Y, Z,) inside teh struct file A.
Now, to call array 'X' from the each struct file I am using A.X; B.X; C.X; D.X. Though this is working fine and in my code everytime I have to change X and Y manually wherever thats been called. Is there any aletrnative way, essentially I am looking for something like
S = X;
A.S
B.S
C.S
D.S
  댓글 수: 3
Turbulence Analysis
Turbulence Analysis 2022년 12월 14일
It's in the form of 1x1 struct
Stephen23
Stephen23 2022년 12월 14일
편집: Stephen23 2022년 12월 14일
"I have sturct files named 'A' , 'B' , 'C' ,'D' etc."
A file is some data saved on a storage device (e.g. hard-drive): https://en.wikipedia.org/wiki/Computer_file
A structure is a data class used in MATLAB memory: https://www.mathworks.com/help/matlab/structures.html
What is a "struct file" ? I have never heard of such a thing, nor is it clear how such a thing would even exist, or be used.

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

채택된 답변

Jan
Jan 2022년 12월 14일
S = 'X';
A.(S)
B.(S)
C.(S)
D.(S)
This is called "dynamic field names".
  댓글 수: 3
Stephen23
Stephen23 2022년 12월 14일
편집: Stephen23 2022년 12월 14일
"e.g if I have number 60 stored in some variable C, is it possible to use like this"
It is certainly possible, if you want to force yourself into writing slow, complex, inefficient, obfuscated, insecure, buggy code that is hard to debug:
Best avoided.
Use indexing, just like MATLAB is designed for.
Jan
Jan 2022년 12월 14일
@Turbulence Analysis: Stephen23 hits the point. Read and consider the linked tutorial carefully.
T3 = A_60.X2 looks like you have hidden the indices 3, 60 and 2 in the names of the variables or fields. This makes it much harder to access the fields, while using real indices is trivial:
T(3) = A(60).X(2)

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

추가 답변 (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