Accessing (loaded) Data, with a known variable Variable name. How do I avoid Eval?

2 次查看(过去 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
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.
Albert Yam
Albert Yam 2012-8-15
Thanks for the response. My code works, just I don't like the eval use. Poked around and would have to loop through the structure to find Data (and Time) as fieldnames, but due to the size (and number of subsystems) I'm just going to leave it as is.

请先登录,再进行评论。

采纳的回答

Daniel Shub
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.
  1 个评论
Albert Yam
Albert Yam 2012-8-22
Thanks for the response, the data is not a single variable per se. Just the format that the signal logging saves the data in. (The example is what comes out of the 'unpack all').
The purpose of the script is to load the signals logged, do some comparisons and plot. The example in the question is just a stripped down version.
Digging around the logsout this is what I get.
logsout.my_model_main_block.my_model_IN.Subsys1_my_subsystem.my_signal_1.data
which after the command
logsout.unpack('all')
The workspace has (which is a time series structure)
my_signal_1
I have a lot of subsystems and signals in the model, and the unpack nicely puts them all in the workspace (but not in the aforementioned 'S'). I don't know the signal names directly. Just that inputs all have 'in_*' (and outputs 'out_*'). Which is why I have the 'who' in the example.
I did just that, commented a lot in the code. It works, and not sure what else to really do to improve the code. Was mainly just curious if there was a way to remove the eval use.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Model Verification 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by