Accessing (loaded) Data, with a known variable Variable name. How do I avoid Eval?
조회 수: 4 (최근 30일)
이전 댓글 표시
Hello, I'm looking to avoid eval. There are loaded variables that I would like to analyze, I do not know the names of the variables being loaded. The names can be found, but here is the problem.
varloaded.data = rand(1,5); %loaded from MAT, see below
varnames = who('v*');
data = eval([varnames{1} '.data']); %how to avoid this?
plot(data)
Now about the loading, the 'varloaded' is from a siglog in simulink. So what I actually load is
S = load('myfile.mat','logsout')
S.logsout.unpack('all')
Due to the unpack, the variables are not loaded into 'S'. So alternatively, is there was a way to 'unpack' into a variable? This would allow me to use the usual variable fieldname method.
Matlab2007a, Simulink6.6
댓글 수: 3
Walter Roberson
2012년 8월 15일
I do not see any other obvious method, but perhaps looking at the code for the unpack method (if it is available) might suggest possibilities.
채택된 답변
Daniel Shub
2012년 8월 22일
While there might be a work around, it is not clear that you need one. The EVAL function is not inherently evil, it is just generally used poorly. This is a case where it seems to make sense. I am not a simulink user, but this question suggests that eval is more acceptable with simulink.
You are using eval to poof a single variable (data). If you add a few comments about why you are going this route, then you should be able to debug problems in the future. To help with debugging further, I would probably hide it in a function specifically designed to load simulink siglog files.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Simulink Environment Customization에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!