从函数在基础工作区中创建结构(从二维数组的工作区块安抚)
Creating structures in base workspace from function (appease from workspace block for 2D arrays)
我需要从我的一个函数内部在基础工作区内创建结构并用值填充它们,问题是我没有明确知道结构的名称。为什么?根据 Mathworks 文档,我的模型使用工作区块,二维数组需要结构形式。见下文。
%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
有没有人知道如何进行这项工作?
我的解决方案
for InputIndex
%2D arrays must be in structure form for from workspace blocks (Mathworks)
%make structure in base workspace and populate with values
assignin('base','InputIndex',InputIndex);
assignin('base',SimulationData.Input(InputIndex).Name,'[]');%create the signal/structure name give random value for now
evalin('base', sprintf('%s.time=Time;',SimulationData.Input(InputIndex).Name));
assignin('base','Values',SimulationData.Input(InputIndex).Values);
evalin('base', sprintf('%s.signals.values=Values;',SimulationData.Input(InputIndex).Name));
assignin('base','Dimensions',[size(SimulationData.Input(InputIndex).Values,1) size(SimulationData.Input(InputIndex).Values,2)]);
evalin('base', sprintf('%s.signals.dimensions=Dimensions;',SimulationData.Input(InputIndex).Name));
end
我需要从我的一个函数内部在基础工作区内创建结构并用值填充它们,问题是我没有明确知道结构的名称。为什么?根据 Mathworks 文档,我的模型使用工作区块,二维数组需要结构形式。见下文。
%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
有没有人知道如何进行这项工作?
我的解决方案
for InputIndex
%2D arrays must be in structure form for from workspace blocks (Mathworks)
%make structure in base workspace and populate with values
assignin('base','InputIndex',InputIndex);
assignin('base',SimulationData.Input(InputIndex).Name,'[]');%create the signal/structure name give random value for now
evalin('base', sprintf('%s.time=Time;',SimulationData.Input(InputIndex).Name));
assignin('base','Values',SimulationData.Input(InputIndex).Values);
evalin('base', sprintf('%s.signals.values=Values;',SimulationData.Input(InputIndex).Name));
assignin('base','Dimensions',[size(SimulationData.Input(InputIndex).Values,1) size(SimulationData.Input(InputIndex).Values,2)]);
evalin('base', sprintf('%s.signals.dimensions=Dimensions;',SimulationData.Input(InputIndex).Name));
end