I am trying to use ‘chi2pval’ function in my code but it’s a private function to stats. How can I make it available to me?
chi2pval is a private function that's part of the Stats toolbox. This function does exist in MATLAB, but you aren't able to call it directly as it's located in a private folder that isn't accessible by you... at least not normally. What you can do is search for where the source file is located. You can do that typing in the following command into your Command Prompt:
which chi2pval -all
which determines where a particular MATLAB function you are looking for is located on your machine. The -all flag displays the paths to all functions that you're looking for. On my Mac OS X machine, this is what I get:
/Applications/MATLAB_R2013a.app/toolbox/stats/stats/private/chi2pval.m % Private to stats
You can take a look at the source by invoking edit in front of that string that contains the full path to the function, as well as the function name itself:
edit /Applications/MATLAB_R2013a.app/toolbox/stats/stats/private/chi2pval.m
When I do this, I see the source code of chi2pval.
Now, what you can do from here is if you want to actually call chi2pval, you can copy the M-file from this directory to where you are calling your main code, then go ahead and run your code.
Hope this helps!
Related
I’m struggling to understand how to call a Matlab function from Labview. I’m using Labview 2020 SP1 and I’ve created the following m file (saved to my desktop):
function A = test()
A{1,1}=ones(1,3);
A{1,2}=ones(1,4);
end
In Labview I’ve added a Matlab script node and imported the function. However when I click run I get the following error:
Error occurred while executing script. Error message from server: ??? Error: Function definition not supported in this context.
Functions can only be created as local or nested functions in code files.
I was expecting the function to be able to run but do nothing as I’ve not added any outputs to the Matlab script node.
Can anyone point me to where I’m going wrong? I’ve looked through the Labview examples but they only seem to demonstrate calling a Matlab script not a function.
Firstly the function would need to be in MATLAB's path (which typically wouldn't include the current user's desktop). MATLAB's path is a list of file locations that MATLAB will search in to find a function definition.
You can view/edit the current path by typing pathtool into MATLAB's interactive command window which will bring up a GUI.
Then your MATLAB script node should be able to call the function as below - note I have added an output just to check the result of the call.
This image is a "LabVIEW Snippet" so you should be able to drag and drop it directly into the block diagram of a new VI and then save and run the VI.
I want to run multiple scripts from different folder by using one script
for example I have the following code:
Original_AddRun1 = 'F:\UPT\Root\Run\S1.m';
Original_AddRun2 = 'F:\UPT\Root\Run2\S2.m';
Original_AddRun3 = 'F:\UPT\Root\Run3\S3.m';
Original_AddRun4 = 'F:\UPT\Root\Run4\Subfolder\S4.m';
run(Original_AddRun1);
run(Original_AddRun2);
run(Original_AddRun3);
run(Original_AddRun4);
there are four scripts that I want to run (S1.m, S2.m, S3.m and S4.m) which are located in the folders (Run, Run2, Run3 and Subfolder inside Run4)
the above Matlab is created inside the "Root" folder that have all the folders
however when i run the code the following error happens
Undefined function or variable 'Original_AddRun2'.
Error in AllRun (line 7)
run(Original_AddRun2);
the first script runs and I get the figure plot I want but the it stops when it tries to run the second script, each individual script works perfectly and were tested before
keep in mind that S2.m needs the variables generated by S1.m to work and the same with S3.m that needs S2.m's generated variables and S4.m needs S3.m's generated variables... this is why those script needs to run in order and Function cannot be used because it uses it's own workspace and not the general workspace
I used save() to save the general workspace at the end of each script then clear the general workspace to be able to run the next script then used load()
in that script to load the variables I need to the general workspace again to use them
I feel there is a much simpler way to do it that the roundabout way save() and load()
I think it is easier to avoid clear as much as possible. It should only be used at the top of a main script. The solution is to use a function workspace (by creating a function) instead of the global workspace. In this way we avoid cluttering the global workspace.
main.m:
clear all
Original_AddRun1 = 'F:\UPT\Root\Run\S1.m';
Original_AddRun2 = 'F:\UPT\Root\Run2\S2.m';
Original_AddRun3 = 'F:\UPT\Root\Run3\S3.m';
Original_AddRun4 = 'F:\UPT\Root\Run4\Subfolder\S4.m';
run(Original_AddRun1);
run(Original_AddRun2);
run(Original_AddRun3);
run(Original_AddRun4);
F:\UPT\Root\Run\S1.m:
function S1()
... % global variables (ex. Original_AddRun1) are not defined here
... % newly defined variables are not added to global scope
end
I found that when you run a .m script from another .m script you have to clear the workspace before running another .m script in the same process.
Since each script depends on the variables of the previous script in the question that is not directly possible. To avoid this issue and still achieve the objective it is possible to use the save() function to save the workspace, then clear it to run the next script, and then load it again with load().
A bit roundabout way of doing it but it works.
I have tried to read many documentations (especially http://blogs.mathworks.com/loren/2008/08/11/path-management-in-deployed-applications/ where the question is asked but not answered to) on compiled Matlab GUI but was unable to find an answer to my question.
I want to create a compiled GUI in Matlab (compiled with deploytool and that can run on a computer that does not have Matlab), where at some point, the user can specify his own matlab .m file (say for example : myProfile.m) and the Gui uses it later on (this last point is the tricky part).
myProfile.m is some simple function (it takes one argument and outputs one value) that can be located wherever the user wants, and that is totally user defined. I give here a simple example:
function [y] = myProfile(x)
y = x^2;
end
but it can be more complex.
In the Gui I ask the user the path to his profile function and I try to make it a function handle :
Button1 = uicontrol('String','Browse path to your Profile',...
'Position',[320 10 150 150],...
'Callback',#button1_Callback);
function [profileFunc] = button1_Callback(varargin)
[ProfileName,ProfilePath] = uigetfile({'*.m'},'Select your profile');
addpath(ProfilePath);
profileFunc = str2func(strcat('#',ProfileName));
% profileFunc will be used later on in the code
end
Of course, after compilation this code doesn't work, and I get the following error:
'C:\Users\...\myProfile.m' is not in the application's expanded CTF archives at
'C:\Users\...\mcrCache8.0\myGui'. This is typically caused by calls to ADDPATH ...
I know that using addpath in a Gui does not work when you compile the Gui. But if I don't add the path, the program can not find the user provided myProfile.m . So how can I solve this?
Thanks,
Sam
When viewing this from a licensing point it is very simple. Mathworks can not allow to deploy such code, you could easily deploy your own command line version of matlab which runs arbitriray code and does not require any license.
In my opinion there is only one way to go: Deploy m-code and ask the user to install matlab or octave.
Alternative:
If you deploy a jar, the JRE is already running. Concider using java script as the JRE already brings the script engine. Then the user would have to input java script.
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 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.