creating structures in base workspace from function (appease from workspace block for 2D arrays)
Afficher commentaires plus anciens
Hi guys, I need to create structures inside the base workspace from inside one of my functions and populate them with values, the problem is I don't explicitly know the names of the structures. Why? My model uses from workspace blocks, 2D arrays need structure form, as per Mathworks documentation. See below.
%some function
function [SimulationData,Error,Warnings] = ExtractTestCaseData(TestCaseFile,TestCase,OutputType)
.
.
[NumericData,TextData,RawData] = xlsread(TestCaseFile,TestCase);
.
.
%logic to create and populate simulationData structure with data from excel file
.
.
if regularVector
assignin('base',SimulationData.Input(InputIndex).Name,SimulationData.Input(InputIndex).Values(1,:)); %no problem
elseif 2DArray
%now what? I need in the base workspace
%var.time=[TimeValues]
%var.signals.values=[DataValues]
%var.signals.dimensions=[DimValues]
assignin('base',SimulationData.Input(InputIndex).Name,'1');%create signal name with random value, I don't know the value of Name, need to convert to structure form as in the comments
evalin('base','nameIDontKnow.time=SimulationData.Time;'); %two problems actually, referencing a name I don't know in base workspace and base workspace has no concept of what SimulatinData.Time is, and so on if you catch my drift.
.
.
end
Does anyone have an idea of how to make this work?? Thanks Dan
5 commentaires
daniel
le 8 Fév 2017
Although popular with beginners, dynamically accessing variables makes code slow, buggy, hard to read, hard to debug, and has other disadvantages too:
Better ways of passing data between workspaces are given in the documentation:
Note that "Best Practice: Passing Arguments", and the worst method is using evalin, assignin.
daniel
le 8 Fév 2017
daniel
le 20 Fév 2017
Stephen23
le 20 Fév 2017
Sounds like like a good situation for using nested functions. That would make it easy to solve, and avoids the whole eval / evalin / assingin hassle.
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Naming Conventions dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!