calling gui function in a non-gui m file - Matlab - matlab

So I have been trying to call a button callback function in test_gui.m function in non-gui m file say test2.m.
But I fail all the time. I tried using findall or findobj but still I get a problem. could you tell me if there is anything I have to do?

You cannot access any functions in an m file except for the function by which the file is named.
For example, you have an m file named func.m which contains functions func(), helper_func1(), and helper_func2(); only func() will be available to any code outside of that file. See http://www.mathworks.com/help/matlab/ref/function.html: "Local functions are only available to other functions within the same file."
What you want is to create a new file named helper_func1.m containing helper_func1() and then call that function from test2.m as well as inside test_gui.m.

Related

Detach anonymous function from file in which it's defined

I am looking for a workaround for the following problem:
Create a script foo.m containing fun = #(x)(x*x)
Run foo. This creates the variable fun.
Delete foo.m
Try running fun(2).
In recent versions of MATLAB (I am using R2019b) this results in an error:
Previously accessible file "foo.m" is now inaccessible.
Somehow, the anonymous function is tied to the file in which it was defined.
Is it possible to somehow "detach" it so that it would continue working even after the file was deleted?
For those curious why I need this, it is for fixing MATLink, the Mathematica/MATLAB interface, for recent versions of MATLAB.
You can use func2str and str2func to construct a new function handle that will work:
more_fun = str2func(func2str(fun));
more_fun(2)
Note that after this, fun can still not be found, but you can also assign to fun directly to make it accessible again.
Edit: just found a (documented) limitation: if the anonymous function uses outside variables, this method will not work, because
Function handles created using str2func do not have access to variables outside of their local workspace or to nested functions. If your function handle contains these variables or functions, MATLABĀ® throws an error when you invoke the handle.

Matlab function working only in debug mode

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.

Using function "Who" within a function in MATLAB

I am using Matlab R2013a and I am trying to use the 'who' function within a function to retrieve a list of variables that begin with a name.
Let's say I have a list of variable in my workspace as follows:
a = 1
a_2 = 2
a_3 = 3
when I run this:
who('a*');
it works fine.
But when I run the same thing inside a function like this:
function someFunction()
who('a*');
end
or
function someFunction()
disp(who('a*'));
end
It doesn't. No error, just no output.
If I had saved those variables in a Matlab file called
myVariables.mat
and run this within the same function like so:
function someFunction()
who('a*','myVariables');
end
It still doesn't work.
I can understand why the first might not work because of scope, but specifying the file to run the 'who' function on should work... what am I missing?
Any help would be appreciated.
Regards
Diaa
As mentioned by #Daniel, the workspace of a function is separate from the base workspace. There are two ways you could use who inside a m-file to inspect the base workspace:
Use a script instead of a function (i.e. omit the function- line; launch the script by its file name as you do with a function): A script shares the base workspace, and thus, who will be able to see all your variables.
Use evalin: evalin('base','who')
You are trying to access variables within a function. Only the input arguments and global variables are visible within a function. You have to do something like:
function someFunction(a1,a2)
who('a*');
end
If you are really trying to use dynamic variable names, I would strongly recommend to change your design.

MatLab global function issue

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

Load code for a MATLAB function block at Simulink runtime

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.