Extracting instance of a variable from a cell in a MAT-file

1 vue (au cours des 30 derniers jours)
Avtar Rekhi
Avtar Rekhi le 24 Jan 2017
I have a script that runs a Simulink model repeatedly, and saves the inputs(some are randomly generated) and outputs of that run into cells in MATfiles. I want to be able to specify a run and load the corresponding outputs into the workspace. I can crudely extract the data, but I don't yet know how to label it as desired.
Each time the Simulink model is run it would output variables such as :
cat = [1 2 3];
dog{1} = [5 7 7];
dog{2} = [5 2 66];
What the script does is write directly to a MATfile for each variable on each run. I.e. There is a 'cat.mat' and 'dog.mat'. If the script did 100 simulations, loading cat.mat results in a cell array called cat, with dimensions [1 100]. cat{1} is the array from run 1, cat{2} is the array for run 2. Equally for dog (except this has more layers). Note I have 300+ variables and some are structures.
Issue 1 (minor):
I was hoping to be able to load part of the MATfile (using v-7.3) but it doesn't support cell indexing. I can get around this by loading the cell with () brackets, and then extracting it again with {} as an extra step.
Issue 2 (main issue):
I need to be able to evaluate the model at a given run. So I need to variables to have the exact names in the workspace. What I would like is to be able to request run 4, and to get workspace variables:
cat = cat{4} from the cat.mat file
dog = dog{4} from the dog.mat file
I have an array with the names of all the variables to cycle through (e.g. saveVar(1) = 'cat'; saveVar(2) = 'dog';) if that helps.*
Currently:
simRun = 4; % The run I want to extract data for.
for k = 1:length(saveVars)
mSaves{k} = matfile(saveVars{k});
% This creates a MATfile object in each cell of mSaves, such that mSave{1} is a MATfile object for the first variable
eval(saveVars{k}) = mSaves{k}.(saveVars{k})(simRun);
% LHS is trying to get the names of the variables I want. RHS is extracting the cell corresponding to the desired simulation run.
end
This treats 'eval' as a variable name and so it doesn't give me the results I want. Any advice or alternative approaches are welcome! I can also change the way the data is saved if necessary. Using MATLAB 2015a currently for compatibility with the model.

Réponses (1)

Avtar Rekhi
Avtar Rekhi le 24 Jan 2017
I have been able to get the data labelled as I wanted with the following work around, altering the for loop from above:
for k = 1:length(saveVars)
mSaves{k} = matfile(saveVars{k});
eval([saveVars{k},'= mSaves{k}.(saveVars{k})(1,simRun)']);
eval([saveVars{k},'=',saveVars{k},'{:}']);
end
But I will leave this open in in the hope that someone can tell me a better way or point out issues with the way I have done this.

Catégories

En savoir plus sur Workspace Variables and MAT-Files dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by