NXC Import File in float-array

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
domsel
Posts: 4
Joined: 27 Sep 2012, 10:26

NXC Import File in float-array

Post 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);
}
Last edited by domsel on 27 Sep 2012, 11:27, edited 2 times in total.
h-g-t
Posts: 552
Joined: 07 Jan 2011, 08:59
Location: Albania

Re: NXC Import File in float-array

Post by h-g-t »

Best to post your code so we can see what you are doing.
A sophistical rhetorician, inebriated with the exuberance of his own verbosity, and gifted with an egotistical imagination that can at all times command an interminable and inconsistent series of arguments to malign an opponent and to glorify himself.
domsel
Posts: 4
Joined: 27 Sep 2012, 10:26

Re: NXC Import File in float-array

Post by domsel »

Done. See above.
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC Import File in float-array

Post 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;
domsel
Posts: 4
Joined: 27 Sep 2012, 10:26

Re: NXC Import File in float-array

Post 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!
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: NXC Import File in float-array

Post 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
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
domsel
Posts: 4
Joined: 27 Sep 2012, 10:26

Re: NXC Import File in float-array

Post 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
Post Reply

Who is online

Users browsing this forum: No registered users and 12 guests