Access to struct atrribute for changing names

조회 수: 1 (최근 30일)
Christian Kujoth
Christian Kujoth 2019년 4월 25일
댓글: Stephen23 2019년 4월 25일
Hello dear MATLAB forum,
I am working with MATLAB and Simulink as part of my bachelor thesis. The situation is as follows:
I have to calculate a temperature from different test data sets. The test data are each stored in a structure, which is drawn for a machine e.g. as "newMPsOfRun6010_03". Now I have many other test data sets like newMPsOfRun6011_03, newMPsOfRun6012_02 and so on.
I would like to do the calculation for all records in one run. So far I've done this by copying the code for one record and only changing the name of the record. However, this is not so nice and would degenerate with several records.
So I ask you for help on how to do this in a loop. In this way this should be done for example:
MPs = [8;8;8;38]; % Number of measuring points in the data set
runs = {'6010_03', '6011_03', '6012_02', '6014_01'}; % Record name ending
for i = 1 : length(runs)
run = strcat('newMPsOfRun', runs{i});
for j = 1:MPs(i)
T5 = run.Performance.T5.MPVector(j); % Access to Record
end
end
My problem with this is that I can not use the '.' operator to access the attributes of the structure of the new string run.
I hope I could make my problem clear and thank you in advance for your help!
Best regards
Christian K
  댓글 수: 1
Stephen23
Stephen23 2019년 4월 25일
Meta-data (like test names) is also data, so it should be stored in a variable, not forced awkwardly into variable names. Forcing meta-data into variable names just makes accessing those variables slow, complex, buggy, and hard to debug. Read these to know why:

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

채택된 답변

Jan
Jan 2019년 4월 25일
Using names like "newMPsOfRun6010_03" shows, that important information is stored in the name of a variable. This is a bad programming practice, see TUTORIAL: Why and how to avoid Eval
It would be much better to use the contents of the struct to store the information, instead of the name:
machine(1).Run = '6010';
machine(1).SubRun = '01';
machine(2).Run = '6010';
machine(2).SubRun = '02';
...
Now you can search for a specific element of the struct array:
index = strcmp({machine.Run}, '6010') & strcmp({machine.SubRun}, '02');
MPVector = machine(index).T5.MPVector
This is ways better than hiding information is the name of the variables. The rule of thumb is so separate the programm (names of variables belong to the code), the data and the GUI strictly (you do not have a GUI, but this is mentioned for completeness also).

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