I would like to have some file, myfunc.m, in my MATLAB path and somehow load its contents into a MATLAB function block automatically just before the simulation starts. This way, I can use an external editor to write these embedded function, version control them separately as independent files, etc.
Is there a way to achieve this programmatically?
My initial attempt was to try and access the contents of the function block using something like get_param, but I can't seem to gain read/write access to the code itself.
If the target MATLAB Function block doesn't already exist then you can add it as follows (see this SO post):
load_system('eml_lib');
libname = sprintf('eml_lib/MATLAB Function');
add_block(libname,'myModel/myBlockName');
You can then modify the block's code using the Stateflow API:
sf = sfroot();
block = sf.find('Path','myModel/myBlockName','-isa','Stateflow.EMChart');
block.Script = 'Your code goes here';
See also this post on MATLAB Answers.
First, you will need to add the folder containing the m-file to the default path. To do this:
(In the Command window)
Go to File -> Set Path -> Add Folder (choose the folder containing the m-file)
Now, you should use the InitFcn callback in the model properties to call your function. To do this, open the model:
(In the Model window)
Go to File -> Model Properties -> Callbacks -> InitFcn
In the edit box provided for the InitFcn, write the command to call your function i.e. myfunc();
You will have to modify this command as per your function and requirements.
Once done, apply the changes to the Model Properties window and simulate the model.
I am thinking that model callbacks may be a way to do what you want, though I have not used this technique myself.
Related
I am using functions from a MATLAB package ("EEGLAB"). One of the functions I'm using ("pop_selectcomp") creates a GUI. However, when I try to interact with the GUI, an error results: an expected variable (In this case "EEG", a data structure) is not defined. That's odd, because pop_selectcomp has EEG as an input. I discovered that declaring global EEG anywhere in the function stack above my call to pop_selectcomp makes EEG available to it again. This is the structure of my function stack.
Main Script
data import function loads variable "EEG"
function to process data
function call to pop_selectcomp
So declaring EEG as a global in the main script or the data processing function fixes the problem.
My interpretation is that when pop_selectcomp creates its GUI window, it's being created outside of my function stack in the main workspace or something like that. So, the EEG variable is only available to it if it's been declared global above the function call. I'm not very familiar with Matlab figures and GUIs, but I guess that normally pop_selectcomps doesn't have this problem because it's not called as a sub-function.
Is there any better way to make this work? Can I somehow point pop_selectcomps' GUI at the correct sub-workspace where it will find the variables it needs? I can modify pop_selectcomps if I have to, although that would be messier. The function can be found here:
https://sccn.ucsd.edu/svn/software/eeglab/functions/popfunc/pop_selectcomps.m
I have a function in a separate file
function [ ] = loadModel( model , version )
cd(model.path);
loadPath = strcat(model.name(1,:) , model.versions(version,:), '_results' ) ;
load(loadPath,'-mat');
end
which using model structure and version number as inputs changes directory to the one of the model and loads its results. When I call the function from a m-file there is no error but its seems like none of the lines of the function have been executed e.g. a variable loadPath doesn't even exist. When I put a break point before load(), I see loadPath generated correctly and if I type the last line manually it works fine. Any clues why is this happening?
P.S. I am used to C++, Java and I find the matlab language absolute nighmare
This is because you're only loading the file within the scope of the loadModel function. In MATLAB, variables declared within a function (this includes through file loading) are only defined within that function (barring global variables, assignin, etc). You need to return the results of the file load in order to use the loaded data. For instance:
function data = loadModel( model , version )
cd(model.path);
loadPath = strcat(model.name(1,:) , model.versions(version,:), '_results' ) ;
data = load(loadPath,'-mat');
end
This will load the file into a struct, which is returned to the calling scope. You can then access the loaded data from this struct.
You need to understand the concept of workspace: the function workspace is different from the base workspace (the one you can access from the command line). Your function executes normally and the variable loadPath is created, but in the function workspace, not the base workspace. As your function doesn't return anything you don't have access to it. In debug mode, you get access to the function workspace, so you can see the variable. You need the function to return the variable of interest if you want to access it from the base workspace. I suggest you have a look at the documentation, it's very thorough.
I have been reading someone else's matlab code and I don't know how the code structured. I mean I would like to know the hierarchy of functions, which function uses which function. I am reading the code to figure that out but its taking a lot of time.
So is there any other way I can see this hierarchy without reading the whole thing? To be honest it is starting to get confusing. Maybe MatLab has a built in function for that! I found this:
How can I generate a list of function dependencies in MATLAB?
but this doesn't seem to be helpful!
The MATLAB profiler will show you what functions are called by your code (and much more information to boot) and allow you to click through the hierarchy of function calls. You can either call profile on and then run your code, then call profile off and profile viewer, or you can simply call profile viewer and type a single line of code to run in the edit box at the top.
Use the dependency report provided in MATLAB:
http://www.mathworks.co.uk/help/matlab/matlab_prog/identify-dependencies.html
There are also some tools on the File Exchange, such as fdep.
No idea about a function to show visible or depended-upon functions. However the basic rules are:
1) Only the first function in a .m file (normally has to have the same name as the file itself) is visible outside that file.
2) Any function can see any top level (see 1.) function if the file is in the matlab path. Matlab can show you the path so you know where it's hunting.
3) The order of the path is important, the first instance of a function called foo found in the path will be called. Obviously the current directory is at the top of the path.
3) All functions in a given file can see all other functions in that file.
That's the basics. No doubt there are other rules, and possibly exceptions to this. But that understanding generally serves me well.
Obviously the easiest way to work out which function is being called is to click on it in the editor and open it.
One thing I do is simply place in each function at the beginning fprintf("inside function <name>/n"); and at the end of the function fprintf("leaving function <name>/n"); where <name> is the name of the function.
This will give you a very specific list of which function is being called by which function (based on the order that they appear). Another thing like this would be to place fprintf("function <name1> calling function <name2>/n"); so you can be more explicit about which function is being called by which one.
I'm learning MatLab & hit a roadblock.
I have an interface.fig file with interface.m which is acting as my 'main' GUI window. From there another file; bright.m is called.
The file bright needs to update global variables in the main file as well as call functions, I have worked out the global variables out but cannot call functions.
Tried everything, looked at doing things like:
reDisplay();
evalin('base','reDisplay()');
interface.reDisplay();
interface>reDisplay();
But had no luck.
Only the first function in an M file is callable from outside of that file. If you want your functions to be globally accessible then you need to save them in independent files.
If you need state to be globally accessible between these functions pass them as arguments or consider using an Object Oriented approach to solving your problem.
If a function in Matlab is defined inside an m-file with file name different than function name - then there is no way of calling this function from outside its m-file.
In order for your reDisplay function to be visible to bright.m, you should have this function in its own m-file called reDisplay.m
I have a MATLAB GUI that loads to aid in visually pre-processing data. Essentially it prompts the user to adjust the data range, reduce number of data points, etc... all while providing an updated graph. Upon completion of this work, I want to be able to close out the GUI and pass variables from the GUI to another MATLAB function that does the data analysis. I have found lots of information on how to pass information from a function TO and GUI, but not the other way around.
Any help would be greatly appreciated.
Global variables can cause hard to find bugs. The best solution for your problem (where you want to pass the data directly to another function on close) might be to call the analysis function from the Figure Close Request Function. When the figure your GUI is running in is told to close, it will run the code in this function, which can call your analysis function and have access to the GUI's data.
Matlab GUIs are functions: the code exists in a .m file just like other functions. Like regular functions, they can have return values. You can get as fancy as you want messing with the varargout system, or you can simply return a value, structure, or cell array containing whatever you want. Open up the m-file and edit it to return what you want it to.
Note: If you require special processing when the figure is being closed to generate the appropriate return value, you can reimplement the closeRequestFcn as you see fit.
The easy way: you declare as global variable, where variable stores the data that you want to carry from the GUI to the main MATLAB workspace. Then, you also declare the same global variable on the command window. Hereinafter, variable to be accesible from both scopes, the GUI and the main workspace.
You could also use save or any other alternatives as csvwrite or dlmwrite to store the data into a file, but this doesn't seem to be your case.