creating structures in base workspace from function (appease from workspace block for 2D arrays)

2 Ansichten (letzte 30 Tage)
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 Kommentare
daniel
daniel am 20 Feb. 2017
yeah so I was looking at your links Stephen, still not sure what's up, problem is that function is called inside another function which is in turn triggered by a callback from a gui (a button) so I don't even know where the base workspace is really. That is, passing vars or structures back up the chain of functions just leads to a callback, which is not it. Jeez
Stephen23
Stephen23 am 20 Feb. 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

daniel
daniel am 23 Feb. 2017
thanks Stephen, nested function would be more difficult to implement, the scrip is approx.. 2000 lines I would need to restructure a lot of code. I don't see a major problem with using these functions to make stuff work. My solution below.
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
  2 Kommentare
Stephen23
Stephen23 am 24 Feb. 2017
"I don't see a major problem with using these functions to make stuff work"
The major problem is that you continue to use buggy code, and that you do not gain experience of how to write better, faster, and more robust code. You obviously feel like this is not a problem (beginners usually just want to "make it work"), but just imagine how that job interview will go one day:
  • Interviewer: "How would you solve problem X?"
  • You: "I would use this buggy and slow method that I have used lots of times to "make it work"".
  • Interviewer: "Do you have experience of writing robust and reliable code?"
  • You: "No"
Of course you can write code to "make it work". Anyone can do this.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by