How do I create variables inside of a macro? I've created a macro library with some macros and now I am trying to figure out how to create a local variable that exists inside a macro for the lifetime of the macro. Perhaps theres a way to store that data somewhere else?
While editing the Macro, use the Inputs collection of the Details panel to create a dummy variable of the required type.
Do not check the By-Ref checkbox.
Leave it unconnected when calling the Macro.
Use the variable like any other variable.
If you are not using latent functions like delay or timeline, use functions instead.
I create function libraries anytime I need code reuse.
Related
I'm using a Stateflow chart to generate some code (C action language). I would like to declare a subchart variable as persistent (or static), so the value is remembered the next time the subchart is executed.
A solution is to attach this variable to one of the parent states, but then this variable is visible to all the subcharts, which is not ideal.
Another solution is to create an embedded Matlab function with persistent variables on it, but this is too cumbersome, since I would need to read all the variables in the beginning and write them before leaving the state.
Is there a simpler way to achieve this?
Yes. Open Model Explorer, go to the subchart, add data.
I have a project consisting of multiple nested functions.
For debugging purpose I want to save all internal variables in one way or another, in order to display figures, replay parts of code etc...
I also want to keep this as transparent as possible regarding calculation time.
My first thought was to create a global variable, and store programmatically at the end of each function the inputs and outputs inside the variable as a structure :
globalVariable.nameOfParentfunction_NameOfFunction.nameInput1 = valueInput1;
globalVariable.nameOfParentfunction_NameOfFunction.nameInput2 = valueInput2;
...
globalVariable.nameOfParentfunction_NameOfFunction.nameOutput1 = valueOutput1;
...
Is it possible to use some kind of reflection to get the name and value of inputs/outputs without necessarily parse the file where the function is written?
I found a good but maybe outdated topic about parsing
How do I collect internal signals?
The simple solution is to use save, which will save all variables in the current workspace (the function’s context) to file.
If you want to keep the values in memory, not in a file, you can use names = who to get a list of all variables defined in the current workspace, then use val = eval(names{i}) to get the value of the variable called name{i}.
I would recommend putting all of that in a separate function, which you can call from any other function to store its variables, to avoid repeating code. This function would use evalin('caller',…) to get names and values of variables in the workspace of the calling function.
Note that using eval or evalin prevents MATLAB from optimizing code using its JIT. They also are dangerous to use, since they can execute arbitrary code, however in this case you control what is being executed so that is no a concern.
I have built a matlab function and I want to access one of its variables ,say x in the workspace. If I write x in the workspace it says Undefined function or variable "x"
Using the global modifier will definitely make your variable visible to the workspace, but it will also make it visible to any other function you call that happens to use that variable name. So if you insist on doing it this way, make sure that your variable name is unique.
A better way, in my opinion, is to pass back the value of the variable as a return value from the function, though this may require changes to calling functions.
Other options are detailed here:
http://www.mathworks.com/help/matlab/matlab_prog/share-data-between-workspaces.html
I want to do something like this: a program where at the beginning some data is initialized. Then I open other GUIs where I show some graphics from these data.
Is there any possible way to set some global variables for different GUIs? If not, how could I do it?
As long as you pass the handle of the first GUI to the second, you can access all the properties of GUI#1 from GUI#2 (and vice versa, since the GUI creation method returns the handle to GUI#2).
Alternatively, you can collect the data you want to visualize (as well as the handles of the GUIs) in a handle object, which means it is passed by reference and thus available everywhere.
Finally, you can, of course, create global variables - at the beginning of any function that would like to access these variables you have to declare them as global var1, var2, var3.
Is it possible to retrieve a local variable from a programme-function i've run in matlab? i.e. i want to retrieve a variable from the code, which is not appeared in the outputs.
Thanks in advance
The following describes code to add to the function itself to make the variable available outside the local scope. When you can't change the function, from the outside there is nothing to be done about changing the scope of course (which is intended, correct behaviour!!).
Dirty ways:
global variables
global t
t=2.468;
For scalars, strings, simple values: assign to variables in base workspace using evalin:
t=2.468;
evalin('base', ['var_in_base=' num2str(t) ';']);
Any other variable, use assignin:
A=magic(20);
assignin('base','A',A);
Proper way:
Inspect them during debugging
If you really want them outside the local scope, add them as output variable!!
Look at Declare function. You can access local variables if you return them as return values. If you do not, you can't access them from outside.
So in
function [mean,stdev] = stat(x)
n = length(x);
mean = sum(x)/n;
stdev = sqrt(sum((x-mean).^2/n));
You have access to mean and stdev but there is no way to access n.
I don't know matlab at all, but from programmer's logic
that seems improper and impossible without hacking the code.
That being said, through Google I saw this:
When you call a script from a function, the script uses the function workspace.
Like local functions, nested functions have their own workspaces. However, these workspaces are unique in two significant ways:
Nested functions can access and modify variables in the workspaces of the functions that contain them.
All of the variables in nested functions or the functions that contain them must be explicitly defined. That is, you cannot call a function or script that assigns values to variables unless those variables already exist in the function workspace.
Base and Function Workspace
Not sure if this helps you at all, but it may clarify some points