cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Browse apps to extend the software in the new JMP Marketplace
Choose Language Hide Translation Bar
dg1
dg1
Level III

Replacing Matrix Values

Hi! I am trying to input all my column data into a 8 by 12 matrix in order( filling in the columns first in a downwards manner). There are only 72 values in my column, so I want the remaining values that are not filled to be 0.  I am having a lot of problems trying to get the code below to work. I would appreciate any help!

dt = Open("Example.jmp");
Rows = nrows(dt); names= dt << Get Column Names (String); Table =[]; Table = J(8,12,0); For(i=1, i <= ncol(dt),i++, data = column(dt, names[i]) <<getValues; For(a=1, a <=12, a++, For(b =1, b <=8, b++, Table[a,b] = operation; // i get errors at this portion ); ); //unsure of the correct way to implement the code below operation= For (c =1, c= Rows, c++, DataInput[c] = Data[c]; ); ); Print(Table);
22 REPLIES 22
txnelson
Super User

Re: Replacing Matrix Values

So once you have the data into the matrices, how do you move them to the permanent locations?
Jim
dg1
dg1
Level III

Re: Replacing Matrix Values

Thank you for your help and interest. Mark Bailey's associative array method has worked. Now I am trying to figure out how to put the associative array into a data table so I can export it as a CSV file.
txnelson
Super User

Re: Replacing Matrix Values

I may be a bit redundant, but here is an alternative way of handling this given the requirement to output to a csv file.  It uses data table manipulation to get the data into the correct form, and then writes the data table to a csv file

Names Default To Here( 1 );

// Create some sample data
dt = New Table( "Example",
	add rows( 72 ),
	New Column( "c1", formula( Random Uniform( 1, 500 ) ) ),
	New Column( "c2", formula( Random Uniform( 1, 500 ) ) ),
	New Column( "c3", formula( Random Uniform( 1, 500 ) ) )
);
dt << run formulas;
dt:c1 << delete property( "formula" );
dt:c2 << delete property( "formula" );
dt:c3 << delete property( "formula" );

// Get the list of columns
colNamesList = dt << get column names( string, numeric );

// Loop across the columns and process them
For( i = 1, i <= N Cols( dt ), i++,
	// Create a new table will only the single column
	dtTemp = dt << subset(invisible, selected rows( 0 ), columns( Column( colNamesList[i] ) ) );
	// If there are not 96 rows, add them
	If( N Rows( dtTemp ) < 96,
		rowCount = N Rows( dt );
		dtTemp << add rows( 96 - rowCount );
		For( k = rowCount + 1, K <= 96, k++,
			Column( dtTemp, colNamesList[i] )[k] = 0
		);
	);
	// Create a sequence column to allow for proper splitting of the data
	dtTemp << New Column( "sequence",
		formula(
			x = Mod( Row(), 8 );
			If( x == 0, x = 8 );
			x;
		)
	);
	dtTemp << run formulas;
	
	// Split the data into 8 columns.....if this should be 12 just change the sequence formula above
	dtSplit = dtTemp << Split(invisible, Split By( :sequence ), Split( Column( dtTemp, colNamesList[i] ) ), Sort by Column Property );
	dtSplit << run formulas;
	
	// Close the un needed temporary data table
	Close( dtTemp, nosave );
	
	// Change the output preferences to get rid of headers
	current_pref = Char( Arg( Parse( (Char( Get Preferences( Export settings ) )) ), 1 ) );
	// Set prefs (comma delimited, no headers)
	Pref( Export Settings( End Of Field( Comma ), Export Table Headers( 0 ) ) );
	// Save csv file

	dtSplit << save( "$TEMP\" || colNamesList[i] || ".csv" );
	
	//Restore oribinal prefs
	Eval( Parse( "pref(" || current_pref || ")" ) );
	
	// Close the un necessary table
	Close( dtSplit, nosave );
	
);

Jim

Re: Replacing Matrix Values

Please take a look at this post. You might find it useful in your work with experiments on plates.

Re: Replacing Matrix Values

I suspect that as a new JMP user, your mindset might still be that of a spreadsheet user. I do not know the destination of the CSV file, but you can do much in JMP using a tall table layout.

 

Why do you need the 8x12 layout? Why do you need to export the matrices as CSV files?

 

We might be able to help you use JMP to your advantage if we knew where this analysis was going.

dg1
dg1
Level III

Re: Replacing Matrix Values

I need the layout for my setup and a csv file is the only type of file read by the device

Re: Replacing Matrix Values

See Help > Scripting Index. Click the drop down button and select Functions. Search for As Table() function.

Re: Replacing Matrix Values

Ok, this updated version of the script should do the trick:

 

Names Default to Here( 1 );

dt = Open( "Example.jmp" );

table = Associative Array();

For( c = 1, c <= N Col( dt ), c++,
	data = Column( dt, c ) << Get As Matrix;
	If( N Row( data ) < 96,
		data |/= J( 96 - N Row( data ), 1, 0 );
	);
	table[Column( dt, c ) << Get Name] = Transpose( Shape( data, 12, 8 ) );
);

content = table << Get Contents;
For( t = 1, t <= N Items( content ), t++,
	dt = As Table( content[t][2] );
	Close( dt, Save( content[t][1] || ".CSV" ) );
);

 

Unfortunately, I do not know how to suppress the first line with the header names in the CSV files. Perhaps someone else knows. We could immediately read the file back as text, delete the first line, and save it out again.

dg1
dg1
Level III

Re: Replacing Matrix Values

This worked!! However, I actually wanted to keep the the column names( or in this case content[t][1]) in the csv file as the first line. It currently does not show. Do you know how I can keep it in the first cell of the csv file?

Re: Replacing Matrix Values

Please be patient. I am having difficulty with saving the CSV file for some unknown reason. I am working with Technical Support.

 

While I wait for them to reply, I want to be sure that you want to replace the first row of the CSV file (original data column headers) with just the name of the original data column name.