Error in Publishing a code in matlab - matlab

I published the following code in Matlab, although the desired output was produces, but along with that it also generated an error while publishing. Any idea why?
%% Gaussian kernel function
% Some text
function t0 = kachra(Param)
t0 = Param;
end
Attached is the Published output.
Thanks

You need to specify an input to your function when publishing.
If you select Edit Publishing Options you see the following GUI:
Modify your expression here and then publish it in order for input arguments to be passed.

You need to configure custom settings to publish your code with output result.
[t0] = kachra(100); %the argument is of your choice.
add your command-line input argument manually here:

Related

Matlab script: Display the outputs of my Simulink model

I have the following Simulink model:
I have the following script:
Constant=43;
Constant1=43;
Constant2=43;
Constant3=43;
Constant4=43;
Constant5=43;
Constant6=43;
Constant7=43;
Constant10=43;
Constant11=43;
In1=[1,2,3];
In2=[4,5,6];
t_stop = 10;
T_s = t_stop/1000;
options = simset('solver', 'ode5', 'fixedstep', T_s);
sim('test_lau.slx',t_stop,options)
I want to display and eventually call the outputs Display, Display2 and Display3.
I tried changing the last line to:
[Display, Display2, Display3]=sim('test_lau.slx',t_stop,options)
But get the error:
Number of left-hand side arguments doesn't match block diagram. When specifying that root-level outports are to be returned individually, the number of left-hand side arguments must be 2 (for T,X) plus number of root-level outport blocks
Does anyone know how to extract those outputs from a script?
Thank you in advance for your help!
Thanks to #Ander Biguri and #Wolfie
The answer was using the block "to Worskpace" instead of the block "Display"

Matlab script edit - model recognition

I try to alter the properties of a MatlabFunction on Simulink. The recommended commands here are below.
open_system('my_model');
S = sfroot
B = S.find('Name','myBlockName','-isa','Stateflow.EMChart');
The 'BlockName' is a MatlabFunction on the model my_model. The content of B is '0×1 empty handle' and S.Name gives DefaultBlockDiagram. It seems it does not recognize the opened model.
The correct command is "slroot"

Change Optimoptions to optimset because of matlab R2011a

I am using optimoptions function in MATLAB R2015a. The syntax is as follows :
options = optimoptions('fmincon','Display','iter','MaxFunEvals',3000000);
fx = #(x)modifiedLogLikelihood(x,len,ET,counta,vals,INT);
parameters = fmincon(fx,x0,[],[],[],[],lb,ub,[],options);
I would like to run the same code on R2011a, but I am getting the following error since optimoptions was introduced after R2011a.
Undefined function or method 'optimoptions' for input arguments of type 'char'.
I referred to this document, that suggested me to use optimset.
I tried to take their advice and wrote:
options = optimset('fmincon','Display','iter',3000000);
fx = #(x)modifiedLogLikelihood(x,len,ET,counta,vals,INT);
parameters = fmincon(fx,x0,[],[],[],[],lb,ub,[],options);
But it yields the following error:
Error using ==> optimset at 198
Unrecognized parameter name 'fmincon'. Please see the optimset reference page in the
documentation for a list of acceptable option parameters. Link to reference page.
How can I use optimset to get the equivalent set of parameters as I was able to obtain with optimoptions. I would be very grateful for your help.
It's helpful if you read the documentation page for a function when trying to use it.
With optimset you want to get the default parameters using just the 'fmincon' string as input and then use optimset again to modify just the parameters that you would like to change from their default value.
options = optimset('fmincon');
options = optimset(options, 'Display', 'iter', 'MaxFunEvals', 3000000);

Matlab 'main' function isn't reading local functions

I have a code in matlab (~1000 lines) that constists of about 15 functions. The code runs fine with each function as a different script, but I want to put them all into one file so I can use the publish function more easily. However, when I put them all together my 'main' function isn't recognizing the local functions. Here's what it looks like:
function full_function()
...
values = fvalues(0);
...
end
function values = fvalues(state)
...
end
When I try to run it, it gives me
"Undefined function 'fvalues' for input arguments
of type 'double'.
Error in full_function (line 32)
values = fvalues(0);"
I've looked all over for how to do local functions and I have no idea what I'm doing wrong. If I right-click on fvalues and hit 'open' it even brings me to the correct portion of the code, so I have no idea why full_function cannot read it. Please help! Thanks.

Call graph generation from matlab src code

I am trying to create a function call graph for around 500 matlab src files. I am unable to find any tools which could help me do the same for multiple src files.
Is anyone familiar with any tools or plugins?
In case any such tools are not available, any suggestions on reading 6000 lines of matlab code
without documentation is welcome.
Let me suggest M2HTML, a tool to automatically generate HTML documentation of your MATLAB m-files. Among its feature list:
Finds dependencies between functions and generates a dependency graph (using the dot tool of GraphViz)
Automatic cross-referencing of functions and subfunctions with their definition in the source code
Check out this demo page to see an example of the output of this tool.
I recommend looking into using the depfun function to construct a call graph. See http://www.mathworks.com/help/techdoc/ref/depfun.html for more information.
In particular, I've found that calling depfun with the '-toponly' argument, then iterating over the results, is an excellent way to construct a call graph by hand. Unfortunately, I no longer have access to any of the code that I've written using this.
I take it you mean you want to see exactly how your code is running - what functions call what subfunctions, when, and how long those run for?
Take a look at the MATLAB Code Profiler. Execute your code as follows:
>> profile on -history; MyCode; profile viewer
>> p = profile('info');
p contains the function history, From that same help page I linked above:
The history data describes the sequence of functions entered and exited during execution. The profile command returns history data in the FunctionHistory field of the structure it returns. The history data is a 2-by-n array. The first row contains Boolean values, where 0 means entrance into a function and 1 means exit from a function. The second row identifies the function being entered or exited by its index in the FunctionTable field. This example [below] reads the history data and displays it in the MATLAB Command Window.
profile on -history
plot(magic(4));
p = profile('info');
for n = 1:size(p.FunctionHistory,2)
if p.FunctionHistory(1,n)==0
str = 'entering function: ';
else
str = 'exiting function: ';
end
disp([str p.FunctionTable(p.FunctionHistory(2,n)).FunctionName])
end
You don't necessarily need to display the entrance and exit calls like the above example; just looking at p.FunctionTable and p.FunctionHistory will suffice to show when code enters and exits functions.
There are already a lot of answers to this question.
However, because I liked the question, and I love to procrastinate, here is my take at answering this (It is close to the approach presented by Dang Khoa, but different enough to be posted, in my opinion):
The idea is to run the profile function, along with a digraph to represent the data.
profile on
Main % Code to be analized
p = profile('info');
Now p is a structure. In particular, it contains the field FunctionTable, which is a structure array, where each structure contains information about one of the calls during the execution of Main.m. To keep only the functions, we will have to check, for each element in FunctionTable, if it is a function, i.e. if p.FunctionTable(ii).Type is 'M-function'
In order to represent the information, let's use a MATLAB's digraph object:
N = numel(p.FunctionTable);
G = digraph;
G = addnode(G,N);
nlabels = {};
for ii = 1:N
Children = p.FunctionTable(ii).Children;
if ~isempty(Children)
for jj = 1:numel(Children)
G = addedge(G,ii,Children(jj).Index);
end
end
end
Count = 1;
for ii=1:N
if ~strcmp(p.FunctionTable(ii).Type,'M-function') % Keep only the functions
G = rmnode(G,Count);
else
Nchars = min(length(p.FunctionTable(ii).FunctionName),10);
nlabels{Count} = p.FunctionTable(ii).FunctionName(1:Nchars);
Count = Count + 1;
end
end
plot(G,'NodeLabel',nlabels,'layout','layered')
G is a directed graph, where node #i refers to the i-th element in the structure array p.FunctionTable where an edge connects node #i to node #j if the function represented by node #i is a parent to the one represented by node #j.
The plot is pretty ugly when applied to my big program but it might be nicer for smaller functions:
Zooming in on a subpart of the graph:
I agree with the m2html answer, I just wanted to say the following the example from the m2html/mdot documentation is good:
mdot('m2html.mat','m2html.dot');
!dot -Tps m2html.dot -o m2html.ps
!neato -Tps m2html.dot -o m2html.ps
But I had better luck with exporting to pdf:
mdot('m2html.mat','m2html.dot');
!dot -Tpdf m2html.dot -o m2html.pdf
Also, before you try the above commands you must issue something like the following:
m2html('mfiles','..\some\dir\with\code\','htmldir','doc_dir','graph','on')
I found the m2html very helpful (in combination with the Graphviz software). However, in my case I wanted to create documentation of a program included in a folder but ignoring some subfolders and .m files. I found that, by adding to the m2html call the "ignoreddir" flag, one can make the program ignore some subfolders. However, I didn't find an analogue flag for ignoring .m files (neither does the "ignoreddir" flag do the job). As a workaround, adding the following line after line 1306 in the m2html.m file allows for using the "ignoreddir" flag for ignoring .m files as well:
d = {d{~ismember(d,{ignoredDir{:}})}};
So, for instance, for generating html documentation of a program included in folder "program_folder" but ignoring "subfolder_1" subfolder and "test.m" file, one should execute something like this:
m2html( 'mfiles', 'program_folder', ... % set program folder
'save', 'on', ... % provide the m2html.mat
'htmldir', './doc', ... % set doc folder
'graph', 'on', ... % produce the graph.dot file to be used for the visualization, for example, as a flux/block diagram
'recursive', 'on', ... % consider also all the subfolders inside the program folders
'global', 'on', ... % link also calls between functions in different folders, i.e., do not link only the calls for the functions which are in the same folder
'ignoreddir', { 'subfolder_1' 'test.m' } ); % ignore the following folders/files
Please note that all subfolders with name "subfolder_1" and all files with name "test.m" inside the "program_folder" will be ignored.