Page 1 of 1

NXC Import File in float-array

Posted: 27 Sep 2012, 10:32
by domsel
Hello,
I am trying to import a .csv file I created in Excel which is filled with float numbers.
My wish is for the numbers to be filled into a two-dimensional array.
I have several problems - alternating from not compiling to file-error -1...
Can anybody help?
Cheers
Dominik

EDIT: An example-code:

Code: Select all

//Motor A Range: {0°,820°} X
//Motor B Range: {0°,500°} Y
//Motor C Range: {0°,-950°} Z

#import "/home/dominik/Desktop/TAIS-HiWi/test nbc/testd.csv" datad[3][2]


void move_x(float pwr, int deg)
{
	RotateMotorEx(OUT_A,pwr,deg,1,false,true);
}

void move_y(int pwr, int deg)
{
	RotateMotorEx(OUT_B,pwr,deg,1,false,true);
}

void move_z(int pwr, int deg)
{
	RotateMotorEx(OUT_C,pwr,deg,1,false,true);
}

string tmp[3][2];
float a[3][2];
float pw,b;

task main()
{	
	tmp=FlattenVar(datad);
	UnflattenVar(tmp,a);
	pw=a[0][0];
	move_x(pw,800);
	b=a[0][1];
	Wait(b);
	move_x(pw,-800);
	pw=a[1][0];
	move_x(pw,800);
	b=a[1][1];
	Wait(b);
	move_x(pw,-800);	
	pw=a[2][0];
	move_x(pw,800);
	b=a[2][1];
	Wait(b);
	move_x(pw,-800);
}

Re: NXC Import File in float-array

Posted: 27 Sep 2012, 10:53
by h-g-t
Best to post your code so we can see what you are doing.

Re: NXC Import File in float-array

Posted: 27 Sep 2012, 11:20
by domsel
Done. See above.

Re: NXC Import File in float-array

Posted: 27 Sep 2012, 12:56
by HaWe
tmp is an array at your code
string tmp[3][12];
, so your asignment is faulty.
tmp=FlattenVar(datad);

at least you will have to declare the indices of tmp.
otherwise you'd just use tmp as a simple string var
string tmp;

Re: NXC Import File in float-array

Posted: 27 Sep 2012, 13:45
by domsel
yes that seemed to make sense to me. I tried to follow your advice but got only this far:

Code:

Code: Select all

//Motor A Range: {0°,820°} X
//Motor B Range: {0°,500°} Y
//Motor C Range: {0°,-950°} Z
//Die Verwendung mehrerer verschachtelter tasks dient nicht der Übersichtlichkeit aber der korrekten Abfolge und Verknüpfung der Bewegungen.

#import "/home/dominik/Desktop/TAIS-HiWi/test nbc/testd.csv" datad[3][2]


void move_x(float pwr, int deg)
{
	RotateMotorEx(OUT_A,pwr,deg,1,false,true);
}

void move_y(int pwr, int deg)
{
	RotateMotorEx(OUT_B,pwr,deg,1,false,true);
}

void move_z(int pwr, int deg)
{
	RotateMotorEx(OUT_C,pwr,deg,1,false,true);
}

string tmp[3][2];
float a[3][2];
float pw,b;

task main()
{	
	for(int i=0;i<=2;i++)
	{
		for(int j=0;j<=1;j++)
		{
			tmp[i][j]=FlattenVar(datad[i][j]);
			UnflattenVar(tmp[i][j], a[i][j]);
		}
	}
	pw=a[0][0];
	move_x(pw,800);
	b=a[0][1];
	Wait(b);
	move_x(pw,-800);
	pw=a[1][0];
	move_x(pw,800);
	b=a[1][1];
	Wait(b);
	move_x(pw,-800);	
	pw=a[2][0];
	move_x(pw,800);
	b=a[2][1];
	Wait(b);
	move_x(pw,-800);
}
leads to:
# Status: NBC compilation failed.
# Error: Error parsing expression: datad[__main_7qG2_i_7qG2_001][__main_7qG2_j_7qG2_003]
File "/home/dominik/Dropbox/TAIS-HiWi/test nbc/portalx.nxc" ; line 32
#
#----------------------------------------------------------
# Error: Error parsing expression: tmp[__main_7qG2_i_7qG2_001][__main_7qG2_j_7qG2_003]
File "/home/dominik/Dropbox/TAIS-HiWi/test nbc/portalx.nxc" ; line 34
#
#----------------------------------------------------------
# Error: Error parsing expression: a[__main_7qG2_i_7qG2_001][__main_7qG2_j_7qG2_003]
File "/home/dominik/Dropbox/TAIS-HiWi/test nbc/portalx.nxc" ; line 34
#
#----------------------------------------------------------
# Error: Invalid variable argument: a[__main_7qG2_i_7qG2_001][__main_7qG2_j_7qG2_003]
File "/home/dominik/Dropbox/TAIS-HiWi/test nbc/portalx.nxc" ; line 34
# unflatten a[__main_7qG2_i_7qG2_001][__main_7qG2_j_7qG2_003], __D0main, __constVal0, __constVal0
#----------------------------------------------------------
# Error: Invalid string argument: __constVal0
File "/home/dominik/Dropbox/TAIS-HiWi/test nbc/portalx.nxc" ; line 34
# unflatten a[__main_7qG2_i_7qG2_001][__main_7qG2_j_7qG2_003], __D0main, __constVal0, __constVal0
#----------------------------------------------------------
5 errors during compilation
Thanks for the help so far. It's about time to leave my office now, so I will look for new answers tomorrow morning.
Cheers!

Re: NXC Import File in float-array

Posted: 27 Sep 2012, 14:37
by afanofosc
Unfortunately, the #import pre-processor directive does not work like you hoped it would. This feature lets you import a file as a raw 1 dimensional byte array containing all the bytes in the file that you import. So a file containing

HI THERE

would result in a data array defined like this:

Code: Select all

byte data[] = {'H', 'I', ' ', 'T', 'H', 'E', 'R', 'E'};
So automatically importing a csv file into a multi-dimensional array is not supported via #import.

You might be able to use #include to insert a comma-separated list of floating point numbers into an array initializer but I am not sure that would work either. The problem there would be the requirement for { and } at each dimension level.

John Hansen

Re: NXC Import File in float-array

Posted: 28 Sep 2012, 07:08
by domsel
Hello,

thank you, that helps in a way, because I know now that I am not just too silly to use the commands properly.

I will try working with three files now and then save the three resulting arrays in a two dimensional super-array :)

So long!
Cheers
Dominik