error: can't perform indexing operations for <unknown type> type when sourcing - octave

With this code, saved as test.m
function test()
x = 1;
endfunction
I get the following error message when sourcing it via source(test.m) in the GUI:
>> clear
>> source (test.m)
x = 1
error: can't perform indexing operations for <unknown type> type
error: evaluating argument list element number 1
>>
Calling the function test via >> test works fine, but I'd like to know what I'm doing wrong here.
Progress:
calling just test, in the right dir seems to do it, but then what are we sourcing for?

If you run
source(test.m)
the interpreter tries to evaluate the "." subscript on the variable "test" (which is a function in your case) and then call source with the result.
What you want is to call the function source with the string "test.m" so you have to use quotes:
source ("test.m")
or don't use () in which case all arguments are passed as strings:
source test.m

Related

Retrieving A Function From A WebhookScript Global Variable

In WebhookScript, I can store a function in a variable with:
sub = function(a, b) {
return a - b
}
I'd like to store a function in a Global Variable so that I can use it in multiple Custom Actions. But if I've saved the above function as $sub$ then
sub2 = var('$sub$')
subX = sub(1,2)
causes an error:
Trying to invoke a non-function 'string' # line...
And
function subX(a,b){
var('$sub$')
}
when sub only contains return a - b, doesn't work either.
Obviously I need to convert the string to a function but I'm not sure whether that's possible.
I know this is a bit of an obscure language but if anyone knows how this can be done in similar languages like JavaScript and PHP, I'm happy to test out any guesses...
The solution here is to remove the function section and just enter the script, which inherits the execution scope so if my global variable $script$ is:
return 'hello ' + a
Then I can execute the function with:
a = 'world'
value = exec(var('$script$'))
echo(value)
(credit to Webhook.Site's support team for explaining this)

How to understand tcl's return

I wanted to get the number of parameters of a proc from tcl and didn't want it to be printed in tclsh, so I used return
When I execute the following statement in the c environment
string tclCmd = "info args " + tclProcName + ";return";
Tcl_Eval(interp, tclCmd.c_str());
string res = Tcl_GetStringResult(interp);
The result of the execution is not printed in tclsh, but at the same time it is not possible to get the correct value
So I tried to write it like this:
string tclCmd = "info args " + tclProcName;
Tcl_Eval(interp, tclCmd.c_str());
string res = Tcl_GetStringResult(interp);
Tcl_Eval(interp, "return");
This works, but I don't understand why it doesn't print out immediately when Tcl_Eval is executed, and the return statement is still valid afterwards
The return command is not helpful at all there. It triggers an exception condition that comes out of Tcl_Eval as a return code of TCL_RETURN instead of TCL_OK (that is converted by the outer structure of a procedure, if that is relevant). Instead, you should call Tcl_ResetResult(interp); after you have finished with the return value (or have taken another reference to it) to put things back to how they were before your command implementation was invoked.

How do I pass objects to functions in ooRexx?

I'm a long-time mainframe Rexx programmer who is trying out objects in ooRexx. The results are surprising. For example, here is a program:
#!/usr/bin/rexx
a = .number~new(3.14)
say "a =" a
say "a~val =" a~val
call say_number a
exit 0
say_number:
procedure
parse arg num
say "In say_number"
say "num =" num
say "num~val =" num~val
return
::class number public
::attribute val get public
::method init ; expose val ; use arg val
::method new ; expose val ; use arg val
::method string ; return "'"self~val"'"
The result is:
> number
a = '3.14'
a~val = 3.14
In say_number
num = '3.14'
18 *-* say "num~val =" num~val
8 *-* call say_number a
REX0097E: Error 97 running /home/tony/bin/.scripts/number line 18: Object method not found
REX0476E: Error 97.1: Object "'3.14'" does not understand message "VAL"
It appears that the object is being resolved to its string value before it's passed to the say_number function. Weird! Am I missing something obvious?
Well, that didn't take long. I changed parse to use in the function, and everything worked as expected. Per the Reference manual:
USE ARG retrieves the argument objects provided in a program, routine,
function, or method and assigns them to variables or message term
assignments.
PARSE assigns data from various sources to one or more variables
according to the rules of parsing. ... If you specify UPPER, the
strings to be parsed are translated to uppercase before parsing. If
you specify LOWER, the strings are translated to lowercase. Otherwise
no translation takes place.
Presumably PARSE converts the arguments to a string so that it can change case as requested (or defaulted).

Attempt to perform arithmetic on field 'x' (a table value)

I'm working with lua-alchemy, and I'm setting a global variable in my AS3 code in this way:
_lua.setGlobal("map", _map);
With _map being a object with the following function in it:
public function get x():int
{
return 10;
}
if then I try to do something like this in Lua
local a = map.x + 1
I get the following error:
Lua script failed: luaDoString:21: attempt to perform arithmetic on field 'x' (a table value)
Does anyone knows why it does that, and how I could fix it?
EDIT :
When I print type(map.id), it prints table... Shouldn't it print number?
I found my error. According to this page, I have to use as3.tolua(map.x) to convert it to the right type.

Using functions on callbacks of a GUI

I'm using GUIDE to create an interface where a function [x,y]=function(a,b,c,d) will be executed when the button is clicked. Im having problems to get this to work. GUIDE creates an autogenerated function with the syntax varargout = LineasA(varargin).
I'm calling the GUI using this syntax [x,y]=LineasA(a,b,c,d).
Errors I get are:
Error in ==> LineasA>LineasA_OutputFcn at 73
varargout{1} = handles.output;
??? Error using ==> feval
Output argument "varargout{2}" (and maybe others) not assigned during call to
"C:\Users\ZeTa\Documents\MATLAB\ImagenB\LineasA.m>LineasA_OutputFcn".
Error in ==> gui_mainfcn at 263
[varargout{1:nargout}] = feval(gui_State.gui_OutputFcn, gui_hFigure, [],
gui_Handles);
Error in ==> LineasA at 40
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
Error in ==> ImagenB at 17
[MatrizB,Cuenta]=LineasA(Cuenta,waveformObj,channelObj,MatrizB);
You have to be clear where you are getting the inputs to this function, and where you want the outputs to go. It is proper coding to store the inputs and outputs in the handles struct that is passed into the callback. Also, the proper callback structure is:
LineasA(hObject, eventdata, handles)
However, if you insist on calling and storing from the base workspace, you can do as follows:
LineasA(hObject, eventdata, handles)
% grab values from base workspace
Cuenta = evalin('base', 'Cuenta');
waveformObj = evalin('base', 'waveformObj');
channelObj = evalin('base', 'channelObj');
MatrizB = evalin('base', 'MatrizB');
% the rest of your code
% assign outputs
assignin('base', 'MatrizB', matrizB);
assignin('base', 'Cuenta', Cuenta);
end
However I recommend getting those values in the handles structure and not to use evalin and assignin, they are usually bad coding techniques.