Can't access Range method of Range object; COM limitations? - matlab

In the Excel VBA Editor's Immediate Window, I can do the following:
?ActiveSheet.Range("C3:D4").Range("C3:D4").Address
$E$5:$F$6
According to some simple tests, this doesn't seem to respond the same in Matlab. Here is the code to set up the COM interface for the tests:
excel = actxserver('Excel.Application');
excel.Visible=1;
wbks = excel.Workbooks;
wbks.Add
sht = wbks.Item(1).Sheets.Item(1)
%
% Run some range tests
%
try
excel.DisplayAlerts = 0; % Forgo save prompt on Close
end; try
wbk.Close
end; try
excel.Quit % Excel process still present
end; try
delete(excel) % Excel process disappears
end % try
Now, with sht being a Worksheet object, I get the following error instead:
K>> o=sht.Range('C3:D4')
o = Interface.Microsoft_Excel_14.0_Object_Library.Range
K>> o.Range('C3:D4').Address
Cannot find an exact (case-sensitive) match for 'Range'
The closest match is: range in C:\Program Files\MATLAB\Single_R2015b\toolbox\stats\stats\range.m
Did you mean: K>> o.range('C3:D4').Address
This is the wrong function, since range with an uncapitalized r is the internal Matab function. Hence, I pressed Ctrl-C to break out (otherwise, it complains about the incompatible argument).
To troubleshoot why Range is not recognized, check whether is is a method or property. When accessed via COM, Range is a method rather than a property:
K>> methods(o)
Methods for class Interface.Microsoft_Excel_14.0_Object_Library.Range:
<...snip...>
PrintPreview Table
Range TextToColumns
RemoveDuplicates UnMerge
<...snip...>
This further shows that Range is not a property (even though it is in VBA):
K>> get(o)
<...snip...>
QueryTable: 'Error: Object returned error code: 0x800A03EC'
Resize: [1x1 Interface.Microsoft_Excel_14.0_Object_Library.Range]
Row: 3
<...snip...>
Since properties are listed alphabetically, Range would show up after QueryTable if it was recognized as a property. However, it isn't listed in the above results.
As an alternative diagnostic step, I tried accessing Range using the dot notation (o.Range). Unfortunately, Matlab seems to got for its own native function range, which has nothing to do with an Excel Range.
So after all that diagnostic work....
THE QUESTION
For a given Range object, how does one access the Range method (as recognized by COM) or the Range property (as it is described in the VBA documentation)?
AFTERNOTE
There seem to be many discrepancies with in accessing Range properties and methods via the COM interface. In the Immediate Window, one can use the Offset property (it is desribed as a property):
? ActiveSheet.Range("C3:D4").Offset(2,3).Address
$F$5:$G$6
Over COM, not so much, even though get(o) shows Offset to be a valid property that returns a range:
K>> o=sht.Range('C3:D4')
o = Interface.Microsoft_Excel_14.0_Object_Library.Range
K>> o=o.Offset(2,2)
Index exceeds matrix dimensions.

Here is a solution I've reached by trial and error, refined with some help with Matlab technical support:
Invoke get as a method, then supply the desired property name. It works for property Offset.
K>> o = sht.Range('C3:D4')
o = Interface.Microsoft_Excel_14.0_Object_Library.Range
K>> o.get('Offset',2,3).Address
ans = $F$5:$G$6
It works for Range, too.
K>> o = sht.Range('C3:E5')
o = Interface.Microsoft_Excel_14.0_Object_Library.Range
K>> o.get('Range','B2:C3').Address
ans = $D$4:$E$5
The strange thing is that Range is a method according to COM (but a property according to the Excel VBA documentation). Despite being seen as a method when accessed via COM, it needs to be invoked using get rather than invoke. The latter yields an error:
K>> o.invoke('Range','B2:C3').Address
Error using Interface.Microsoft_Excel_14.0_Object_Library.Range/invoke.
Cannot find an exact (case-sensitive) match for 'Range'.
The closest match is: range in C:\Program Files\MATLAB\Single_R2015b\toolbox\stats\stats\range.m

Related

Using `exist` with structure array

The assignment is
You are testing a functional near infrared (fNIR) device and
measuring how well each subject is able to control the computer mouse using
it. Write a function getcogdata that returns the requested category for
the requested user id. If the requested user does not exist, return an empty
matrix. If no user is requested, return a
cell array of values for all users.
My code is as follows
function output=getcogdata(cat,id)
info=struct('id',{'33','10','48','41'},'name',{'Joe','Sally','Harry','Ann'},...
'age',{'27','25','23','19'},...
'height',{'5.9','6.1','5.8','6.0'},'score',{'9,5','9.3','9.7','9.4'});
if id=='33'
id=1;
elseif id=='10'
id=2;
elseif id=='48'
id=3;
else id=='41'
id=4;
end
output=info(id).(cat)
end
My code works for when measurement and user are specified but I can't figure out how to code for if the user doesn't exist or if no user is requested. I tried not exist(id) I get an error. Is there a way I can use not exist?
Matlab's exist function will tell you if something is known to the current Matlab instance. It won't tell you if a particular value is present.
Your task is probably a little bit more complex than you originally thought but you can accomplish it with a mixture of strcmp, and any.
First, we convert the ids in info to a cell array with
{info.id}
now we can use strcmp to compare them against id
strcmp(id, {info.id})
Finally, we can use any to tell us if any of the values in {info.id} are equal to id. So putting this all together we get
>> info = struct('id',{'33','10','48','41'});
>> id = '33';
>> any(strcmp(id, {info.id}))
ans =
1
We can also this to find the index of id in {info.id} and do away with the if statements in your question by using find instead of any
>> id = '10'; % Present in index 2 - Output should be 2
>> find(strcmp(id, {info.id}))
ans =
2
To answer your final question about not passing the id to getcogdata you can do this using nargin
function output = getcogdata(cat,id)
if (nargin < 2)
fprintf(1, 'No id passed to getcogdata()\n');
end
end
nargin will tell you how many arguments were passed to the function getcogdata.
Thanks to #AndrasDeak for teaching me Matlab's strcmp is much better than the C version I had been assuming it was similar to!
Note: Always read the manual!

set threshold as a function of autoThreshold

I have written a macro for ImageJ/FIJI to deconvolve my confocal microscopy images and run the "3D Object Counter" plugin. The macro successfully runs all required commands and saves all required data in the specified places.
However, I have found that the 3D-OC autothreshold (as shown in the plugin dialog box) is to stringent resulting in objects being lost or divided.
To remedy this I would like to reduce the autothreshold by a predetermined function something similar to what was done here (from:How to get threshold value used by auto threshold Plugin) which resulted in this code:
setAutoThreshold();
getThreshold(lower,upper);
v=setThreshold(lower,upper*0.5);
run("3D Objects Counter", "threshold="v" slice=10 min.=400 max.=20971520 objects statistics summary");
The idea was to call the AutoThreshold values, modify them and set them to a variable. However when these lines are run the following error is returned:
Number or numeric function expected in line 3.
v=<setThreshold>(lower,upper*0.5);
And if the variable is inserted directly into the threshold key for run(3D-OC) the following msg is encountered:
Numeric value expected in run() function
Key:"threshold"
Value or variable name:"setThreshold(lower,upper*0.5"
Any suggestions or help on how to designate the 3D-OC threshold value as a variable as described would be greatly appreciated (as would any work arounds of course :) ).
Cheers
Edit: After testing Jan's response below (which works perfectly), it appears I need to call the threshold set by the 3D-OC plugin. Anyone know how to do this?
The getThreshold(lower, upper) function returns the lower and upper threshold levels in the provided variables. There is no need to assign any value to a new variable, and as you observed, setThreshold does not have any return value.
Instead, you can use the value(s) returned from getThreshold and use them as parameters in the run method (in the correct way, by string concatenation, see here):
setAutoThreshold();
getThreshold(lower, v);
run("3D Objects Counter", "threshold=" + v + " slice=10 min.=400 max.=20971520 objects statistics summary");
Alternatively, you can use &v in the second parameter to avoid string concatenation in the last line (see the documentation for the run() macro function):
run("3D Objects Counter", "threshold=&v slice=10 min.=400 max.=20971520 objects statistics summary");
You might have to use the lower instead of the upper threshold value, depending on whether you count bright or dark objects.

Matlab: How to remove the error of non-existent field

I am getting an error when running matlab code. Here I am trying to use one of the outputs of previous code as input to my new code.
??? Reference to non-existent field 'y1'.
Can anyone help me?
A good practice might be to check if the field exists before accessing it:
if isfield( s, 'y1' )
% s.y1 exists - you may access it
s.y1
else
% s.y1 does not exist - what are you going to do about it?
end
To take Edric's comment into account, another possible way is
try
% access y1
s.y1
catch em
% verify that the error indeed stems from non-existant field
if strcmp(em.identifier, 'MATLAB:nonExistentField')
fprintf(1, 'field y1 does not exist...\n');
else
throw( em ); % different error - handle by caller?
end
end
Have you used the command load to load data from file(s)?
if yes, this function overwrite your current variables, therefore, they become non-existent, so when you call, it instead of using:
load ('filename');
use:
f=load ('filename');
now, to refer to any variable inside the loaded file use f.varname, for
example if there is a network called net saved within the loaded data you may use it like:
a = f.net(fv);
I would first explain my situation and then give the solution.
I first save a variable op, it is a struct , its name is coef.mat;
I load this variable using coef = load( file_path, '-mat');
In a new function, I pass variable coef to it as a parameter, at here, the error Reference to non-existent field pops out.
My solution:
Just replace coef with coef.op, then pass it to the function, it will work.
So, I think the reason behind is that: the struct was saved as a variable, when you use load and want to acess the origin variable, you need point it out directly using dot(.) operation, you can directly open the variable in Matlab workspace and find out what it wraps inside the variable.
In your case, if your the outputs of previous code is a struct(It's my guess, but you haven't pointed out) and you saved it as MyStruct, you load it as MyInput = load(MyStruct), then when use it as function's parameter, it should be MyInput.y1.
Hops it would work!
At first load it on command window and observe the workspace window. You can see the structure name. It will work by accessing structure name. Example:
lm=load('data.mat');
disp(lm.SAMPLE.X);
Here SAMPLE is the structure name and X is a member of the structure

an error in Matlab: Input argument undefined, although variable is existing

I have written this function, and I have already defined values for rg and Lp, but still when I run this function it returns the error : (Input argument "Lr" is undefined.
Error in ==> Bis at 12
if f(Lr,rg,Xo)*f(Lr,rg,Xf)>0)
here is the function :
function[Lp,Xo,Xf]=Bis(Lr,rg)
Xo=0;
Xf=10;
Err=0.01;
syms x;
f=inline('(sqrt((2/3)*(((x*Lr)/3)-(x*x)+((2*x*x*x)/Lr)-((2*x*x*x*x)/(Lr*Lr))+(((2*x*x*x*x)/(Lr*Lr))*exp(-Lr/x))))-rg)');
if f(Lr,rg,Xo)*f(Lr,rg,Xf)>0
disp('The values you entered are not apropriate !')
PlotLpFunction;
Lp='unknown';
elseif f(Lr,rg,Xo)*f(Lr,rg,Xf)==0
if f(Lr,rg,Xo)==0
Lp=Xo;
elseif f(Lr,rg,Xf)==0
Lp=Xf;
end
elseif f(Lr,rg,Xo)*f(Lr,rg,Xf)<0
xi=(Xf-Xo)/2;
while abs(f(Lr,rg,xi))>Err
if f(Lr,rg,xi)*f(Lr,rg,Xf)<0
Xo=xi;
xi=(Xo+Xf)/2;
elseif f(Lr,rg,xi)*f(Lr,rg,Xf)>0
Xf=xi;
xi=(Xo+Xf)/2;
end
end
Lp=xi;
end
The code executes for me on the newest version of Matlab, other than the fact that I don't have the PlotLpFunction.
My initial impression was that you forgot to send the Lr (and all other argument) into you're inlined f function, very easy to fix by adding them as arguments to the inline function. You'll find the full usage in the official documentation.
The relevant part being
inline(expr,arg1,arg2,...) constructs an inline function whose input
arguments are specified by the strings arg1, arg2,.... Multicharacter
symbol names may be used.
but it seems to form the inline just fine by itself on both Matlab 2011b and 2008b, from context presumably. Answer is accepted now, so presumably that was the problem. Can anyone else reproduce his problem? If so please provide your Matlab version or other circumstances.

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.