I'm using this simple code to learn arrays in pine v5:
var float[] my_arr = array.new_float(0)
if barstate.islast
array.push(my_arr, close[1])
a = array.get (my_arr, 0)
plot(a)
I'm trying to insert the previous close in my_arr and plot its value. I think the value of the close[1] should be entered in the array at the index 0 using: array.push() But when I plot that value, It appears to me this message:
The barstate last call will not run until the last bar of the chart. When you load a script it runs on all historical bars first beginning at the first bar in time. The array.get call is trying to pull a value from an array that is not populated. You can put in a ternary check to make sure the array size is greater than 0, or you could initialize your array with a size and a value like (1) which would give the first populated value na until populated by your of statement. If you just want to plot close[1] you can skip the array and just plot that series
Cheers
Related
I'm quite new to programming and GDScript and wondering how to do something that I did think would be quite simple, but I'm struggling a bit!
I've loaded an array of images and I want it to go through each of these images one after the other each time a button is clicked and replace a sprite texture with that particular image.
Right now I can successfully get the sprite to change to any of the images if I put its array number in e.g. new_texture[0] or new_texture[3] etc., but I would like it to go through each of the images one after the other every time the user clicks the button. (And once it's got to the last image, go back to the first image again)
What am I missing?
Here is the code:
extends Node
onready var my_sprite = get_node("/root/Game/Picture/Sprite")
var new_texture = [
load("res://Art/Picture1.png"),
load("res://Art/Picture2.png"),
load("res://Art/Picture3.png"),
load("res://Art/Picture4.png"),
load("res://Art/Picture5.png"),
load("res://Art/Picture6.png")
]
func _on_Arrow_pressed() -> void:
my_sprite.texture = new_texture[0]
As you know, you can get the first image of the array like this:
new_texture[0]
Where new_texture is a variable that holds the array, and 0 is a constant (a literal, to be more precise) that indicates the position on the array.
So you can change the constant to get a different image, for example the fourth element is:
new_texture[3]
Now, that is fine if you want to change which element you get form one version to another of the game, since it requires to modify the codeā¦
But you want which element from the array you get to change during the execution of the program. In other words, you don't want it to be constant, but variable.
So declare a variable that will hold which element you get:
var texture_index := 0
Use it instead of the constant:
func _on_Arrow_pressed() -> void:
my_sprite.texture = new_texture[texture_index]
And now you can change which element from the array you get by changing the value of the variable, for example:
func _on_Arrow_pressed() -> void:
my_sprite.texture = new_texture[texture_index]
texture_index += 1
Here we are changing the value of texture_index so the next time this code executes it gets the next position.
But be careful to keep the value of the variable inside the bounds of the array to avoid errors. So, what do you want to do after it reaches the last element? Do you want it to loop? Well, we can check if we reached the last element with an if, and set a different value to the variable, like this:
func _on_Arrow_pressed() -> void:
my_sprite.texture = new_texture[texture_index]
texture_index += 1
if texture_index == new_texture.size():
texture_index = 0
I remind you that elements of the array go from the position 0 to the position array.size() - 1. For example, if the array has 10 elements, they are in the positions 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 (from 1 to 9, there are 9 numbers, plus the 0 you have 10 numbers). So an array with 10 elements does not have an element in the position 10. So there is no element in the position array.size(), thus, when the index reaches that value it is out of bounds of the array, and we reset it to the first element (position 0).
There are, of course, variation of this approach.
My image array: C_filled = 256x256x3270
What I would like to do is calculate the centroids of each image, and store each centroid corresponding to each 'slice'/image into an array. However, when I try to update the array, like a regular array, I get this error:
"Undefined operator '+' for input arguments of type 'struct'."
I have the following code:
for i=1:3270;
cen(i) = regionprops(C_filled(:,:,i),'centroid');
centroids = cat(1, cen.Centroid);% convert the cen struct into a regular array.
cen(i+1) = cen(i) + 1; <- this is the problem line
end
How can I update the array to store each new centroid?
Thanks in advance.
That's because the output of regionprops (i.e. cen(i)) is a structure, to which you try to add the value 1. But since you try to add the value to the structure and not one of its field, it fails.
Assuming that each image can contain multiple objects (and therefore centroids), it would be best (I think) to store their coordinates into a cell array in which each cell can be of a different size, contrary to a numeric array. If you have the exact same number of objects in each image, then you could use a numeric array.
If we look at the "cell array" option with code:
%// Initialize cell array to store centroid coordinates (1st row) and their number (2nd row)
centroids_cell = cell(2,3270);
for i=1:3270;
%// No need to index cen...saves memory
cen = regionprops(C_filled(:,:,i),'centroid');
centroids_cell{1,i} = cat(1,cen.Centroid);
centroids_cell{2,i} = numel(cen.Centroid);
end
and that's it. You can access the centroid coordinates of any image using this notation:centroids_cell{some index}.
I'm using Matlab to create a GUI. Herefore I'm using the guide function of matlab.
I want to store the slider values in a vector. I am doing this in the callback function:
for i = 1:10
X(i) = get(handles.slider1,'Value');
end
But this results in a vector that stores the same value 10 times. What I actually want is to store the last 10 values of the slider in a vector. Any ideas?
I would suggest to create a 1 x 10 vector of zeros when starting the GUI, i.e. in the OpeningFcn of the GUI:
handles.X = zeros(1,10);
guidata(hObject,handles); % Update handles variable
Then in the Callback function of the slider, you always shift the vector one to the right and add the new value in the first place:
x = get(handles.slider1,'Value');
handles.X = [x, handles.X(1:end-1)];
guidata(hObject,handles); % Update handles variable
Now X always contains the last 10 values of the slider value, X(1) is the last value and so on.
Before the slider hasn't been moved 10 times, some of the values will not be correct, i.e. they will just be zero. If this is a problem, you could grow the X vector dynamically in the Callback.
I have created this text array and image array.
How do i make it such that when a random image appears the corresponding text appears?
Example: When the image shows 9, I want the text displayed to be 9
I really need help.
Are you looking for getting the index of arrays in Lua? I'll just provide a small example.
You can create an array like:
local myArray = {"element_1","element_2",...,"element_n"}
And you can get an array element as:
print(myArray[1]) -- Prints 'element_1' (1st element)
print(myArray[n]) -- Prints 'element_n' (nth element)
So, if you have two arrays as below;
local myQuestionArray = {"1","2",...,"n"}
local myImageArray = {"img_1","img_2",...,"img_n"}
you can take the question and image with respect to a number as:
-- for number = 1
print(myQuestionArray[1]); // 1
print(myQuestionArray[1]); // img_1
-- for number = n
print(myQuestionArray[n]); // n
print(myQuestionArray[n]); // img_n
For mode details, refer: Understanding Lua tables in Corona SDK
Keep Coding.......... :)
I want to plot several curves, each having a different length. So, I've placed each curve as an array in a cell index, Y (this allows me to index through arrays of different sizes inside a FOR loop). I use "hold all" below to enable each iteration of the FOR loop to plot each new array in the cell array Y inside the same plot.
hold all;
for i = 1:1:length(maxusers)
time = increment*(0:1:length(Y{i})-1);
plot(time,Y{i,1,:});
end
While the use of a cell array here does simplify plotting the various curves inside Y, the problem I'm having is creating legend. Currently I'm using a really long/ugly switch statement to cover every possible scenario, but I think there should be a more elegant solution.
If I have an array (where maxusers=4, for example) that is:
filesize = [10 100 200 300];
I know the legend Matlab command that works is:
legend(num2str(filesize(1)),num2str(filesize(2)),num2str(filesize(3)),num2str(filesize(4)));
but I get stuck trying to create a legend command when the number of curves is a variable given by maxusers. Any ideas? Thanks in advance.
Try this:
>> filesize = [10 100 200 300];
>> str = strtrim(cellstr(int2str(filesize.'))) %'# Create a cell array of
%# strings
str =
'10'
'100'
'200'
'300'
>> legend(str{:}); %# Pass the cell array contents to legend
%# as a comma-separated list