We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Moving through values in a string
Page Index Toggle Pages: 1
Moving through values in a string (Read 1363 times)
Moving through values in a string
May 20th, 2010, 12:56pm
 
Hi again, sorry for being a pain, hopefully this will be the last question for today.

I've got my file setup to take data from three .csv files. The strings are then converted to float values and used to drive things on the screen.

I'm currently using a for loop, which is just putting everything on screen at once, which isn't what I want. I want to increment one new value every second (there are 120 strings in my files).

Here's my code at the moment:

Code:
float myValue;


void setup(){

size (1200, 1000);
frameRate(1);

}



void draw(){
String[] time = loadStrings("time.csv");
String[] heart = loadStrings("heart_rate.csv");
String[] speed = loadStrings("speed.csv");


for (int i = 0; i < 120; i++){

myValue = 10 *( Float.valueOf(speed[i]).floatValue());
line(i * 10, myValue * 10, myValue * 5, myValue * 5);


}

for (int a = 0; a < 120; a++){

myValue = 10 *( Float.valueOf(time[a]).floatValue());
ellipse(a * 10, myValue * 20, myValue * 5, myValue * 5);

}


}


What would I need to change to create one ellipse and one square per second?
Re: Moving through values in a string
Reply #1 - May 20th, 2010, 1:16pm
 
First, never (unless you know what you do!) load stuff (strings, images, fonts, etc.) in draw(). It can slow down dramatically your application.
In your case, you don't aim for speed, but yet it is a good habit to take.
Declare the arrays along myValue and do the loading in setup() (removing the declararation/type part, of course).

Note: not sure why you use CSV (that can hold several data per line) and then use three files... Smiley

Next step: make a global index variable. Remove your for loops (keep the content) and increment your index variable at the end of draw(): it will do the job of iteration.
And, of course, use [yourIndex] instead of [i] or [a].

Last note: in Processing you can use float(str) instead of Float.valueOf(str).floatValue()
Re: Moving through values in a string
Reply #2 - May 20th, 2010, 1:58pm
 
Thanks, I wasn't aware that loading things in draw would cause problems. I'm an absolutely beginner when it comes to this kind of programming, so I'd rather not pick up bad habits at this point.

I was struggling with the split command, so I though it would just be easier for me, at this point to call three separate files.

What do you mean exactly by a gobal index variable?

Thanks for all your help, PhilHo, I really appreciate it.

Re: Moving through values in a string
Reply #3 - May 20th, 2010, 2:19pm
 
Mmm, it is faster to show code (untested):
Code:
float myValue;
int index; // The global index variable
String[] time;
String[] heart;
String[] speed;

void setup(){

size (1200, 1000);
frameRate(1);

time = loadStrings("time.csv");
heart = loadStrings("heart_rate.csv");
speed = loadStrings("speed.csv");

}



void draw(){

myValue = 10 *( float(speed[index]));
line(index * 10, myValue * 10, myValue * 5, myValue * 5);

myValue = 10 *( float(time[index]));
ellipse(index * 10, myValue * 20, myValue * 5, myValue * 5);

index++; // Check when reaching 120, stop or restart at 0
}
Re: Moving through values in a string
Reply #4 - May 20th, 2010, 2:32pm
 
It's coming up with a syntax error saying there's a missing right parenthesis, but I can't find where it'd be as they all seem to be there.

The code looks a lot tidier now though, and makes a great deal more sense. Thank you.
Re: Moving through values in a string
Reply #5 - May 21st, 2010, 2:39am
 
Just had to change:

Code:
myValue = 10 *( float(speed[index])); 



into:

Code:
myValue = 10 * (new Float(speed[index])); 



And it's all working now.
Re: Moving through values in a string
Reply #6 - May 22nd, 2010, 12:54am
 
Mmm, a copy/paste of my code doesn't show such compilation error (in 0184).
Don't use new Float()! (creating objects for such calculation is expensive)
Try: myValue = 10 * float(speed[index]); perhaps.
Or, at worse, Float.valueOf(speed[index])
Page Index Toggle Pages: 1