Getting Macro Error: Argument is not optional - libreoffice

I have the following function in my Libre Office macro:
Function izracunajHash(geslo As String, zacetni_hash As Integer)
If zacetni_hash = 1 Then
izracunajHash = 5
Else
izracunajHash = 1
End If
End Function
I call it this way inside Sub Main
print izracunajHash("Geslo", 5)
And it gives me the error Argument not optional on the second line of code If zacetni_hash = 1 Then.
What is the problem here?

Related

'value' undefined near line 3 column 12

I'm learning Computer Vision(& new to Octave/Matlab) and wrote this code in Octave:
function result = func_scale(img, value)
result = value .* img;
endfunction
husky = imread('Husky.jpg');
imshow(func_scale(husky, 1.5));
On running the file I'm getting this error:
error: 'value' undefined near line 3 column 12 error: called from
func_scale at line 3 column 10
I have named the file
func_scale.m
Any idea what I'm doing wrong?
Thanks.
If you create a file func_scale.m with the content
function result = func_scale(img, value)
result = value .* img;
endfunction
and you call it from octave with
func_scale
obviously the parameter img and value are missing for the calculation in line 2.
To do what you want you can leave your func_scale function in the file func_scale.m but move the call (imread... imshow) to another file, for example myfile.m. You can then call this from Octave with myfile
Or create one file foobar.m which starts with 1;, then your function definition and then your function call

Define a function with a function handler as argument

Is it possible to define a function with a function handler as an argument in Matlab?
I've tried with
function x = name(#f,gh)
but I get an error message stating Invalid syntax at '#'.
You cannot use syntax involving # in function definition. The anonymous function handle would do the work:
function x = SO_Example(h,gh)
x = h(gh);
And you can call the function as follows:
SO_Example(#(a)a.^2 , 2)
ans = 4
Or like this:
h = #(a)a.^2;
SO_Example(h,2)
ans = 4
Please, see comments for additional explanations

How do I suppress lines of code inside one function when that function is being called from within another function?

I have two functions, one is called from inside of another. I want certain parts of the first function to not execute when being called within the second function.
function vvec = vecVelocity(varargin);
%must be preceded with a 'syms var real' declaration where var is
%the parameter of your vector function
if nargin > 1 & nargin < 3
r = [sym(varargin(1:end))];
elseif nargin > 3
disp('too many inputs')
return
else r = [sym(varargin(1))];
end
if length(r) < 3
r = [r,0];
end
dr = diff(r);
uT = vecUnitTan(r);
speed = sqrt(sum(dr.^2));
v = speed*uT;
vvec = matlabFunction(v);
disp(['Simplified Symbolic Form: ' char(simplify(sym(vvec)))]);
I would like to suppress this last line disp(...) from appearing when I call this following second function from the command window
function speed = vecSpeed(r);
%must be preceded with a 'syms var real' declaration where var is
%the parameter of your vector function
v = sym(vecVelocity(r));
sp = sqrt(sum(v.^2));
speed = matlabFunction(sp);
disp(['Simplified Symbolic Form: ' char(simplify(sym(speed)))]);
At the moment, calling the vecSpeed function, causes a bunch of statements to be displayed from other preceding functions that are called from within the vecSpeed function (and some that are called from within the vecVelocity function), but I only want the disp(...) statement from the vecSpeed function to be called, not any of the others.
In the first function, you can put a block of codes in
if numel(dbstack) == 1
% Your code block
end
to prevent them to be executed unless the function is being called directly from Command Window.
Another way is to check for existence of some variables like debug_1, debug_2 etc.. which you can pass from the 2nd function when you call the 1st function.

SAS IML use of Mattrib with Macro (symget) in a loop

In an IML proc I have several martices and several vectors with the names of columns:
proc IML;
mydata1 = {1 2 3, 2 3 4};
mydata2 = {1 2, 2 3};
names1 = {'red' 'green' 'blue'};
names2 = {'black' 'white'};
To assign column names to columns in matrices one can copypaste the mattrib statement enough times:
/* mattrib mydata1 colname=names1;*/
/* mattrib mydata2 colname=names2;*/
However, in my case the number of matrices is defined at execution, thus a do loop is needed. The following code
varNumb=2;
do idx=1 to varNumb;
call symputx ('mydataX', cat('mydata',idx));
call symputx ('namesX', cat('names',idx));
mattrib (symget('mydataX')) colname=(symget('namesX'));
end;
print (mydata1[,'red']) (mydata2[,'white']);
quit;
however produces the "Expecting a name" error on the first symget.
Similar question Loop over names in SAS-IML? offers the macro workaround with symget, what produces an error here.
What is the correct way of using mattrib with symget? Is there other way of making a variable from a string than macro?
Any help would be appreciated.
Thanks,
Alex
EDIT1
The problem is in the symget function. The &-sign resolves the name of the matrix contained in the macro variable, the symget only returns the name of the macro.
proc IML;
mydata1 = {1 2 3};
call symputx ('mydataX', 'mydata1');
mydataNew = (symget('mydataX'));
print (&mydataX);
print (symget("mydataX"));
print mydataNew;
quit;
results in
mydata1 :
1 2 3
mydata1
mydataNew :
mydata1
Any ideas?
EDIT2
Function value solves the symget problem in EDIT1
mydataNew = value(symget('mydataX'));
print (&mydataX);
print (value(symget("mydataX")));
print mydataNew;
The mattrib issue but persists.
SOLVED
Thanks Rick, you have opened my eyes to CALL EXECUTE() statement.
When you use CALL SYMPUTX, you should not use quotes for the second argument. Your statement
call symputx ('mydataX', 'mydata1');
assigns the string 'mydata1' to the macro variable.
In general, trying to use macro variables in SAS/IML loops often results in complicated code. See the article Macros and loops in the SAS/IML language for an indication of the issues caused by trying to combine a macro preprocessor with an interactive language. Because the MATTRIB statement expects a literal value for the matrix name, I recomend that you use CALL EXECUTE rather than macro substitution to execute the MATTRIB statement.
You are also having problems because a macro variable is always a scalar string, whereas the column name is a vector of strings. Use the ROWCAT function to concatenate the vector of names into a single string.
The following statements accomplish your objective without using macro variables:
/* Use CALL EXECUTE to set matrix attributes dynamically.
Requires that matrixName and varNames be defined at main scope */
start SetMattrib;
cmd = "mattrib " + matrixName + " colname={" + varNames + "};";
*print cmd; /* for debugging */
call execute(cmd);
finish;
varNumb=2;
do idx=1 to varNumb;
matrixName = cat('mydata',idx);
varNames = rowcat( value(cat('names',idx)) + " " );
run SetMattrib;
end;

Type mismatch when tring to extract the date by calling a function in macro

I am using the below code. I am getting the "Type Mismatch : Error 13" when i am trying to execute it. I tried by putting all variable type (long, double, date, string) but non of them worked out.
Function DefectCreateDate()
Dim j
Dim DateString
Dim CreatedDate
j = GetClm("Created Date")
i = 2
DateString = "19-03-2013 21:41:01"
DefectCreateDate = DateValue(DateString)
End Function
Sub testnewde()
Dim K
Dim j
k= 2
j = DefectCreateDate(k)
MsgBox (j)
End Sub
You have an errant parameter 'k' being passed into DefectCreateDate; its defintion is parameterles.