From: Dave ba on
Hi
We are going to use Matlab a lot at my school. And right now I’m making a program in it.
But I stumble on a problem.
Its like this:

It is a video program, and you can click with the mouse on every frame to make a dot.
The coordinates of the dot are stored in two variables.
Zx and Zy (but I'll only use the Zy, here)
Then you can plot a graph in a axes to see the y-coordinate against the time(frames).
This is the code for the x-axes of the graph:
Code:
T = 1:1:number-of-frames-in-the-video;

I create a variable here for the y-axes of the graph:
Code:
Y(number-of-frames-in-the-video, 1) = 0;
Y(the-current-frame-number, 1) = Zy;


Then the plot:
Code:
Plot(T, Y);

This all works, but it only draws the dot of the current frame in the graph, and I want it to also
Draw the previous clicked dots.
But when the data of one dot is stored, and you click again the previous data is automatically erased because of the line:
Code:
Y(number-of-frames-in-the-video, 1) = 0;

but I need the line to create the variable.

Lets say the total number of frames is 5
The Time variable would be: 1 2 3 4 5
And the y-coordinates variable would be:
0 0 0 0 0
Then you click on the second frame, the y-coordinates variable could change in:
0 34 0 0 0
But then you click again on the third frame, it would change in:
0 0 25 0 0
Instead of, what I want: 0 34 25 0 0

So, if anyone knows how to fix this, it would help me a lot.
From: dpb on
Dave ba wrote:
....

> ...and I want it to also Draw the previous clicked dots. But when
> the data of one dot is stored, and you click again the previous data is
> automatically erased because of the line: Code:
> Y(number-of-frames-in-the-video, 1) = 0;
> but I need the line to create the variable.

Well, don't _do_ that if that isn't what you want... :)

Remove the initial allocation to outside the callback function before
the individual points are selected

> Lets say the total number of frames is 5 The Time variable would be: 1 2
> 3 4 5 And the y-coordinates variable would be: 0 0 0 0 0 Then you click
> on the second frame, the y-coordinates variable could change in: 0 34 0
> 0 0 But then you click again on the third frame, it would change in: 0 0
> 25 0 0 Instead of, what I want: 0 34 25 0 0
> So, if anyone knows how to fix this, it would help me a lot.

Use a counter variable that is incremented each point/click and use that
variable in the array index to store the value

W/o actual code to work with can't see what you actually did, but

indx = 0; % initialization somewhere before the clicking starts

% on click event
indx = indx+1; % increment the counter
y(indx) = new_value;

Actually, if you have enough in the code at present to create the vector
y as you demonstrated above as

[0 34 0 0 0] followed by [0 0 25 0 0]

whatever variable it is that you're using to put the value in the n-th
position now is the index variable; simply reference the y-array entry
individually and quit re-initializing every time as noted first by
putting the initial allocation outside the click-event code.

--
From: Dave ba on
thank you very much. I see what you mean. and I will try it out tomorrow (right now I dont have a computer with matlab on it). I hope I can get it right, cos not very experienced with matlab. but thank you, thank you, thank you
From: dpb on
Dave ba wrote:
> thank you very much. I see what you mean. and I will try it out tomorrow
....

Good. Glad it makes sense of what to do even if the "how" may still be
a little bit puzzling. The latter just goes w/ the territory of
beginning programming, whatever the actual language.

A couple of things to watch out for...

First, you will want to preallocate the array before beginning the
selection process or you'll run into array bounds errors or w/o doing so
rely on Matlab to reallocate dynamically for you behind the scenes.
This will not be an issue for small array sizes and user interaction as
the overhead speed penalty won't be noticeable but it's a good habit to
get into from the git-go as it can become a real performance bottleneck
when you get into actual sizable processing code as your usage intensifies.

y = zeros(SomeMaxValue,1); %preallocate y array

or similar can suffice. You'll want your event code to check that the
point to be stored is within the array bounds and take appropriate
action on that condition (either simply inform user array is full if
that's all that's needed and ignore the input or allocate additional
room or whatever is needed for the purpose).

Note that initially there's the old conundrum of "how big is 'big
enough?'" and altho ML has the facilities to relieve the developer from
specifically answering that question, to ignore it entirely eventually
leads to the above mentioned problem of performance as one progresses.

Also, a fairly minor detail but note that for convenience you can
reference a 1D array w/ only one subscript even though ML considers it a
2D array w/ the second dimension of unity.

That is, y(idx) is the same as y(idx,1) This is mostly "syntactic
sugar" but it does make the code intent more clear as well as eliminate
a little extra typing.

Oh, in looking at your original posting again, I see the

T = 1:1:number-of-frames-in-the-video;

so you do know the max in this case from that. That's good; I just
wrote the previous on the thinking of a user arbitrarily selecting some
set of points. In this case you'll simply want to bound the user input
to avoid the indexing problem and won't need to reallocate, etc.

Most will write it as

T = 1:number-of-frames-in-the-video;

on the same basis as the comment regarding the superfluous array
index--the default spacing is one so the 1:1:N isn't needed, specifically.

--
From: Dave ba on
thank you dpb, for your help. although size is a important issue for a program, my main focus is still on the program's lacking of saving the previous data.
i've tried to reposition the code of creating the variable to somewhere before the clicking. but this variable musst become a global, to request it later on.
so the code that says the variable is zero is gone, but is now replaced by requesting the variable, which eventually still put's it back to zero after a point is clicked.

I'm clearly doing something wrong here, but I cant figure out what..

but because files say more than words, I've added the files:
http://uploading.com/files/45bm85cb/oefening11.fig/
http://uploading.com/files/5c9m465e/oefening11.m/

and here is a video to test the program with:
http://uploading.com/files/339bmadc/plflex%2Bsprong%2B50fps.avi/

(after you click the free download, you'll see a timer at the top. after a minute you can download the file)

the program is in another language (Dutch).
but this is how it works:
"push button" = select a video file
the slider can be used to walk through the video
clicking on the video will create mass points
"zwaartepunt" = will create the mass center of all the points clicked
"wis" = cant be used yet
"restart" = restarts the program
"plot grafiek" = plots a graph of all the y-coordinates of all the mass centers
and this is also where most of the code is located, where it goes wrong.

thanks in advance
 |  Next  |  Last
Pages: 1 2 3
Prev: Generalized Ridge Regression
Next: 2d dct type-1