What does translate() with multiple parameters do? - openscad

I accidentally left off the square brackets in a translate vector. Instead of causing an error, OpenSCAD silently ignored the error.
Is there some special meaning to translate() with multiple parameters? What should the second line do? I've attached an image showing the results I'm getting.
translate([5,5,-25]) color("red") cube([10,10,50]);
translate(5,5,-25) color("blue") cube([10,10,50]);

The translate "moves" one object from one Cartesian point to another.
The translate function always expects an array at his first parameter (named v, the array with our x,y and z coordinates).
Any function call in openscad can be written without the parameter name UNLESS you do use an different argument position.
So, using the translate function as example:
translate(0)
// ignored, first parameter is not an array.
cube([5,5,5]);
translate(v=5)
// ignored, v is not an array.
cube([5,5,5]);
translate([10,10,10])
// normal call.
cube([5,5,5]);
translate(v=[10,10,10])
// named parameter call.
cube([5,5,5]);
translate(1,2,3,4,5,6,7,8,9,0,infinite,v=[15,15,15])
// it works! we named the parameter, so
// openscad doesn't care about it's position!
cube([5,5,5]);
translate(1,2,3,4,5,6,7,8,9,0,[20,20,20])
// ignored, the first parameter is not an array
// AND v is not defined in this call!
cube([5,5,5]);
// At this point there are 3 cubes at the same position
// and 3 translated cubes!
test();
test(1);
test(1,2);
test(1,2,3);
test(1,2,3,4);
// 01) There is no function overwrite in openscad (it doesn't care about the number of parameters to
// 02) The function names are resolved at compile time (only the last one will be recognized).
module test(p1,p2,p3) echo( "test3" );
module test(p1,p2) echo( "test2" );
module test(p1) echo( "test1" );
OpenScad uses this syntax everywhere, not only in the translate function call.
Now your two lines:
translate([5,5,-25]) // executed, cube moved to x=5,y=5,z=-25
color("red") // executed
cube([10,10,50]); // executed, default creation pos x=0,y=0,z=0
translate(5,5,-25) // ignored, cube not moved.
color("blue") // executed
cube([10,10,50]); // executed, default creation pos x=0,y=0,z=0

Related

Encounter with a strange Mask initialization Command

So in one of the mask in the model i came across an initialization command that i don't understand.
The logic is for an SR Flip Flop. That latches the output.
It has 1 parameter that takes in the initial state for output 1(Output is o and o(bar)).
Which has the following properties:
Name: MaskParam1
Value: 0
Type: Edit
Evaluate: True
Tunable: True
The command is written in the Initialization tab of the Mask Editor.
ini = (#1~=0);
The first thing that came to my mind was anonymous function handle, that returns value of the parameter. In this case would be 0.
So as (0 ~=0) = 0hence the initial output of SR will be 0.
When i type the following in matlab:
ini = (#1~=0);
I get an error of unexpected matlab expression.
So I tried another thing. Considering as the Parameter is refering to the masked parameter so if we take a variable:
x = 0;
% The default value for the mask parameter
ini = (#x~=0);
This will give an error that
Error: "x" was previously used as a variable, conflicting with its use here as the name of a function or command.
So my question is ini
ini = (#1~=0)
a function or a variable??enter code here

My mex function ignores my if statement

I have a mex function that takes in a field of a struct in the third input (i.e. prhs[2]), which is a boolean. If true, it will parse information from the fourth input (i.e. prhs[3]). In a nutshell, this is the code excerpt:
mxValue = mxGetField(prhs[3], 0, "change"); mxLogical *change;
change = mxGetLogicals(mxValue);
mexPrintf("true/false: %i \n", *change);
mexEvalString("drawnow;");
if ( change ) {
mexPrintf("...Parsing info... \n");
mexEvalString("drawnow;");
mxValue = mxGetField(prhs[3], 0, "info");
nRows = mxGetM(mxValue); nCols = mxGetN(mxValue);
Eigen::Map<Eigen::VectorXd> info((double *)mxGetPr(mxValue),nRows);
}
As you can see, I do a printout to see whether the input prhs[2] is true or false. Even if the function prints out false, the if statement gets executed regardless, because I can see the printout ...Parsing info....
Why is my MATLAB mex function ignoring my if statement?
C is not MATLAB! C is C!
You are checking if pointer change has a value. It does indeed have a value, a memory direction e.g. #72BA21, to the location where the value of the boolean is stored.
You can either check the contents of whats inside that specific direction if(*change) as #buzjwa suggest, or grab the information on the array, instead of a pointer to it, using mxGetData.
As a side note: learn to debug, or at least, print statements. a simple mexPrintf() call would have shown you what change contains

Str2Func matlab doesnt run the function

I am having a problem using str2func, when matlab says that the function i called to is
'undefined function 'X' for input or arguments of type 'double'.
There is a string which is stored in Shape.
Shape gets its string from a function, which gives Shape different values randomly.
I have a function for each shape, so for example if Shape gets the value square, there is also a function named square, and i want to call that function according to the string that is stored in Shape.
so i wrote this:
f=str2func(shape);
f(x0,y0);
function [] = vav(x0,y0)
x=[x0 x0+1 x0+1 x0+2 x0+2 x0];
y=[y0 y0 y0+3 y0+3 y0+4 y0+4];
patch(x,y,[0.3 0.2 0.45])
end
so shape gets a string
for example shape='vav';
but when i use f=str2func(shape)
and i call f(x0,y0) it doesn't work.
how can i fix it? and what is wrong with this way?
Thanks!!
More code:
draw(shape)
function[]= draw(shape)
x0=5;
y0=20;
set(myfig,'KeyPressFcn',#keyPressed);
function [] = keyPressed(~,event)
switch event.Key
case 's'
y0=moveDown(y0);
cla(PlayAx)
f=str2func(shape);
f(x0,y0);
if y0==0
draw(shape)
end
and the error for example when shape gets the value 'vav' it gets also other value but it works for none of them
Undefined function 'vav' for input arguments of type 'double'.

Matlab: Call function that has no outputs from an anonymous function

I'd like to call a certain function from within an anonymous function, as in
#(){fooBar(baz)}
Trouble is, fooBar has no outputs, which makes the anonymous function complain. Is there a way around this besides making the fooBar function return a dummy output?
The problem is in your anonymous function definition. By enclosing your function foobar(baz) between the characters {...}, you are programming a function which has to :
evaluate the expression foobar(baz)
Place the result of this expression into a cell
return the cell
Obviously in step (2) Matlab cannot place the result of the expression (1) in a cell because there is no output from (1).
So simply define your function without the curly braces:
myFunction = #() fooBar(baz)
and everything should work ok.
To demonstrate with an example, let's define the function fooBar by doing something which does not produce an output (change an axe limits for example):
fooBar = #(axlim) set(gca,'XLim',axlim)
I can now call fooBar([0 20]) and the current axes will directly have its axes limits set to [0 20]
If there is an axis span which I use often ([-5 5] for example), I could be tempted to define a new function which will always call fooBar with the same (often used) parameters:
fooBarPrefered = #() fooBar([-5 5])
Now every time I call fooBarPrefered(), my axes X limits are directly set to [-5 5].
To further prove the point, since calling fooBar([-5 5]) does not produce an output, Matlab will indeed complain if I define my function with curly braces:
fooBarPrefered = #() {fooBar([-5 5])} ;
>> fooBarPrefered()
One or more output arguments not assigned during call to "set".
Error in #(axlim)set(gca,'XLim',axlim)
Error in #(){fooBar([-5,5])}
But note that this is the same error than if you were trying to assign the output of fooBar to a variable directly in the workspace:
a = fooBar([0 20])
One or more output arguments not assigned during call to "set".
Error in #(axlim)set(gca,'XLim',axlim)
Bottom line: If a function does not have an output, do not try to redirect this output to a variable or an expression.

Understanding the syntax

I have this couple of lines which is difficult to understand..
oframes1 = do_localmax( difofg.octave{o}, 0.8*thresh, difofg.smin ) ;
oframes = [oframes1 , do_localmax( - difofg.octave{o}, 0.8*thresh, difofg.smin)] ;
here,
do_localmax is a function
thresh is a variable
difofg is also a function
I understand that the 1st line calls the function and passes the parameters but it is difficult understanding the second line and also what kind of syntax is difofg.octave{o}
Syntactically:
difofg is not a function; it's a variable, probably a struct or a class object. difofg.octave and difofg.smin get the element named octave or smin from that struct/object.
difofg.octave is apparently a cell array, and difofg.octave{o} gets the oth element of that cell array.
The second line creates an array with two elements: the first is oframes1, and the second is the result of the second call to do_localmax. Maybe this equivalent code will make it clearer what's happening:
oframes1 = do_localmax( difofg.octave{o}, 0.8*thresh, difofg.smin);
oframes2 = do_localmax( -difofg.octave{o}, 0.8*thresh, difofg.smin);
oframes = [oframes1, oframes2];