how can I use a variable in your code? I am thinking of writing:
example:
I have a variable x = Math.random (4);
I have a function: timedFunctionb1, timedFunctionb2, timedFunctionb3, timedFunctionb4
Now I want to add X to the function name:
timedFunctionb .....
as there add the variable name to use it when setTimeout (timedFunctionb.x, 5000); it does not work like that. I can not find the correct definition.
Put all functions in an array
var functions:Array = [timedFunctionb1, timedFunctionb2, timedFunctionb3, timedFunctionb4];
Then pick a random element:
setTimeout (functions[Math.floor(Math.random() * functions.length)], 5000);
Related
I am trying to generate an output as a random number using Postman so that I can PUT it onto a 'thing' in my IoT app
If I give the value in the following format, it works correctly:
{
"WindSpeed" : "88"
}
But now I want to pass on the value of the "WindSpeed" in an automated manner (something like using the random value function) so that I don't have to manually change it every time,
Unfortunately, I am not able to do so as I have trying ways available online including setting global variables etc. etc. but it is always giving an error of 'BAD STRING' or that the JSON content does not have 'ValidProperties' etc. I think that maybe my syntax is wrong. Could someone please guide me as to how I can generate random values in postman(syntax etc.)?
but why not just to use
postman.setEnvironmentVariable("random_list_name", _.random(1,
10000000))
Where "random_list_name" Environment Variable
This is simple and seems does the same
You shall generate your random value in the prescript tab using a function like this one:
// random generator function
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min;
}
// generate the random value
const myval = getRandomInt(0,100);
// set the value into the global variable
pm.globals.set('value', myval);
// to see it in console
console.log(myval);
Then, in your JSON body, you shall use it:
{
"Windspeed":{{value}}
}
This should work.
I have 3 functions. When the user presses the 'E' key I want it to select one of the functions (at random), I feel like it has something to do with math.random but I can't figure it out.
You don't use math.random to select a function; you use it to pick a random number, which you can then use as an index to get the function you need from a table (as one example):
local list = { function() print(1) end, function() print(2) end, function() print(3) end }
math.randomseed(os.time()) -- don't forget to seed or you likely to get the same sequence
for i = 1, 10 do list[math.random(#list)]() end
I have three functions and i want two variables to run through all the functions. I tried doing this:
R = rot(mir(sca(P(1,:),P(2,:))));
however i get this error:
Error using mir (line 2)
Not enough input arguments.
Any suggestions?
%rot.m
function rot = rot(x,y)
rot = [ cos(pi/6)*x-sin(pi/6)*y; sin(pi/6)*x+cos(pi/6)*y ];
%mir.m
function mir = mir(x,y)
mir = [x;(-y)];
%sca.m
function sca = sca(x,y)
sca = [2*x;2*y];
You should not be surprised about the error. Function mir expect two parameters (in fact, all of your functions expect that), but you provide only one. Mind you, a matrix is considered one parameter. You can do either of the following to correct the problem:
Redefine mir to accept one parameter and split it inside the function into two separate variables
Redefine sca to return two values:
function [outx, outy] = sca(x, y)
outx = 2 * x;
outy = 2 * y;
and then pass them to mir like so:
[scax, scay] = sca(x, y);
mir(scax, scay);
Obviously, the same needs to be done to function rot as well.
In MATLAB if you have more then one output argument you have to explicitly specify the output variables. By default function always returns one (the first) argument.
In your situation one choice can be to change definitions of your functions in such a way that they receive only one input argument as a matrix. For example:
%mir.m
function mir = mir(xy)
mir = [xy(1,:); -xy(2,:)];
or even easier in this case (you can simplify other functions as well):
function xy = mir(xy)
xy(2,:) = -xy(2,:);
I hope you got the idea.
Then you can run:
R = rot(mir(sca(P(1:2,:))));
If you cannot change your function definitions for some reason, you will have to split the one-line call to three function into 3 lines:
S = sca(P(1,:),P(2,:));
M = mir(S(1,:),S(2,:));
R = rot(M(1,:),M(2,:));
I have an .m file in matlab named PowerMinimiser and two functions in it
function PowerMinimiser
PowerOut = fmin(minFunction,0,100);
display(PowerOut)
end
and
function PowerOut = minFunction(varargin)
RunMode = 2;
ThresholdValue = 10;
if nargin > 0
ThresholdValue = varargin{1};
end
%Receive PowerOut value from .main file and pass in ThresholdValue and
%RunMode values:
[PowerOut] = main(ThresholdValue,RunMode);
end
Now what I would like to do is use the matlab fmin function so that I can find a value for the variable ThresholdValue which will give the lowest value possible for the variable PowerOut. The value for ThresholdValue is a number between 1 and 100, and is passed into a function in the main.m where a number of calculations are done and then a value for PowerOut is outputted.
Using the minFunction function I am able to receive and pass the variable fine, using the line
[PowerOut] = main(ThresholdValue,RunMode);
but I am not sure how to use the fmin function to get the value of ThresholdValue that gives the lowest value for PowerOut. I would like the ThresholdValue value to be shown once the fmin has done the necessary calculations. How can I do this? Any help would be greatly appreciated.
Thanks
You should use function fminbnd, which replaced fmin function in newer versions of Matlab. To pass function as an argument to fminbnd use # in front of the function name, like this:
PowerOut = fmin( #minFunction, 0, 100 );
I have two functions:
function [] = func_one()
S.pb = uicontrol('style','push','unit','pix','posit',[20 20 260 30],
'string','Print Choices','callback',{#func_two,S});
and I have the second function:
function [a] = func_two(varargin)
a = 'alon';
end
I want func_one to return the variable a of func_two. How can I do that please?
I tried:
function [a] = func_one()
But I guess I have to do something with 'callback',{#func_two,S})
Thank you all!
If, as you say, you want func_one to return the value a in func_two then the easiest way to do this without using a callback is:
function [a] = func_one()
S.pb = uicontrol('style','push','unit','pix','posit',[20 20 260 30],
'string','Print Choices');
a = func_two()
The above will allow you to say run a=func_one and a will be the string 'alon'.
If you really really want func_two() to be a callback of your pushbutton, and you want a='alon' to be assigned in the workspace of func_one (the function that calls func_two) then put this in func_two
assignin('caller','a',a)
And if neither is what you want, then maybe you can indicate why you want func_one to return what func_two returns - like the exact interaction you are hoping to have with your GUI and how it differs from what you're actually experiencing.
If you are designing a GUI programmatically, I suggest you use nested functions to share data. Example:
function IncrementExample()
x = 0;
uicontrol('Style','pushbutton', 'String','(0)', ...
'Callback',#callback);
function callback(o,e)
%# you can access the variable x in here
x = x + 1;
%# update button text
set(o, 'String',sprintf('(%d)',x))
drawnow
end
end