Allow user to name structure?

조회 수: 1 (최근 30일)
Ibro Tutic
Ibro Tutic 2017년 5월 10일
댓글: Ibro Tutic 2017년 5월 10일
I realize variables should not be named dynamically, but I would like to name structures dynamically. We have large data sets for various vehicles where I think dynamic naming would help for easier storage of the structures as mat files.
For example, lets say we have some data for a dodge vehicle. I would like the user to be able to input 'Dodge' and then the structure would be Dodge.Data1, Dodge.Data2, etc. Then when the structure is saved as a mat file, it is saved as 'Dodge'.
This would allow the user to easily access structures for Dodge, Ford, Chevy, etc. for example.
The other way that I could think of is have a separate field for the specific model within the structure, like Car.model = 'Dodge'. The resulting structure here would be called Car. I feel like this would make it more difficult for a user to quickly locate the .mat file that has the 'Dodge' data in it.
Any ideas?

채택된 답변

Matt J
Matt J 2017년 5월 10일
편집: Matt J 2017년 5월 10일
You can do this.
sname='Dodge';
S.(sname).Data1=...
S.(sname).Data2=...
and then when you want to save to a .mat file you do
save filename.mat -struct S
However, what you are pursuing sounds excessively acrobatic to me. You could just have different .mat files for different cars. Then, if you want to read data for a particular car into a structure, you could just do things like,
carData=load('Dodge.mat')
  댓글 수: 1
Ibro Tutic
Ibro Tutic 2017년 5월 10일
Actually, you are completely right. I mistakenly thought that I couldn't save the .mat file off as what ever I wanted. So I could just have a structure that is Car.Data1, Car.Data2, and save the corresponding mat file off as "Dodge.mat" or whatever application the data corresponds to.

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

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