# Setting the Data Set to a 2D matrix

**URL:** <https://forum.inductiveautomation.com/t/setting-the-data-set-to-a-2d-matrix/25310>\
**Category:** Ignition\
**Tags:** scripting\
**Created:** [May 24, 2019, 7:49pm UTC](https://forum.inductiveautomation.com/t/setting-the-data-set-to-a-2d-matrix/25310 "2019-05-24T19:49:11Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ammermanbrittanyf](https://avatars.discourse-cdn.com/v4/letter/a/45deac/32.png) [@ammermanbrittanyf](https://forum.inductiveautomation.com/u/ammermanbrittanyf)\
**Post date:** [May 24, 2019, 7:49pm UTC](https://forum.inductiveautomation.com/t/setting-the-data-set-to-a-2d-matrix/25310/1 "2019-05-24T19:49:11Z")

</div>

Hello,

I am fairly new to ignition, but have a lot of previous programming experience. I have created master, a variable that parses through an XML file to create a 2D array. I am now trying to show that 2D array in a table in ignition. I know the array is correct as I can get it to output to an xml file. However, I am unable to figure out how to take an empty table and set the data set to my matrix. I believe I can also do this row by row or column by column, but would prefer not to as the matrix is rather large.

```auto
#TODO Add it to the table
table = event.source.parent.getComponent("PwrTable")
#event.source.parent.getComponent('PwrTable').data = system.dataset.clearDataset(table.data)
master = system.dataset.toDataSet(master)
event.source.data = master 
#system.db.refresh(table, "PwrTable")
#table = system.dataset.addColumn(table, scaninfo_label, "Scanned Info Labels", str)

```

With the few lines not commented out above, I get the following error:

```auto
Traceback (most recent call last):

  File "<event:actionPerformed>", line 47, in <module>

TypeError: toDataSet(): 1st arg can't be coerced to com.inductiveautomation.ignition.common.script.builtin.DatasetUtilities$PyDataSet
Ignition v7.9.6 (b2018012914)
Java: Oracle Corporation 1.8.0_211

```

Thank you in advance for your help.

---

<div class="post-metadata">

**Author:** ![grietveld](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/grietveld/32/6984_2.png) [@grietveld](https://forum.inductiveautomation.com/u/grietveld)\
**Post date:** [May 24, 2019, 7:59pm UTC](https://forum.inductiveautomation.com/t/setting-the-data-set-to-a-2d-matrix/25310/2 "2019-05-24T19:59:07Z")

</div>

> [@ammermanbrittanyf](#):
>
> master = system.dataset.toDataSet(master)

Are you sure that you want to try and convert 'master' to a dataset? Based on the small sample of code, I can't tell what master is configured as. I'm thinking based on the previous line it should be 'table'

---

<div class="post-metadata">

**Author:** ![ammermanbrittanyf](https://avatars.discourse-cdn.com/v4/letter/a/45deac/32.png) [@ammermanbrittanyf](https://forum.inductiveautomation.com/u/ammermanbrittanyf)\
**Post date:** [May 28, 2019, 12:31pm UTC](https://forum.inductiveautomation.com/t/setting-the-data-set-to-a-2d-matrix/25310/3 "2019-05-28T12:31:06Z")

</div>

I think you are correct. I am trying to input my matrix into the table. However, I thought I had to set it to the data set first to do so.

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [May 28, 2019, 2:55pm UTC](https://forum.inductiveautomation.com/t/setting-the-data-set-to-a-2d-matrix/25310/4 "2019-05-28T14:55:19Z")

</div>

Really need to see some more code, to understand what variable type you end up with after the parsing through the XML.

Either way, assuming that master is a dataset type, then all you need to do is set the table’s data property to the dataset.

```auto
table.data = master

```

You have this script in an event what component is the script on?

If the data that you are working with is in fact an array then you will also need to have a header list to properly create the dataset.

```auto
headers = ['x','y']
data = [[10,15],[15,20],[20,25]]

#the number of columns in headers must match the number of columns in each row of data
dsExample = system.dataset.toDataSet(headers,data)

#will create a dataset with the following format
# x | y
# 10 | 15
# 15 | 20
# 20 | 25

```

---

<div class="post-metadata">

**Author:** ![ammermanbrittanyf](https://avatars.discourse-cdn.com/v4/letter/a/45deac/32.png) [@ammermanbrittanyf](https://forum.inductiveautomation.com/u/ammermanbrittanyf)\
**Post date:** [May 28, 2019, 3:03pm UTC](https://forum.inductiveautomation.com/t/setting-the-data-set-to-a-2d-matrix/25310/5 "2019-05-28T15:03:35Z")

</div>

I have included the code below to help. Thank you for your assistance.

```auto
path = system.file.openFile(".xml")
#TODO Error Proofing with no file selected
if path is not None:
	data = system.file.readFileAsString(path)
	system.tag.writeToTag("FA_Intern_Brittany_Playground/Text2", data)

		
	tree = ET.parse(path)
	root = tree.getroot()
 	
	w = 100
	h = 100
	#creates a list containing 10 lists, each of 15 items, all set to 0
	master = [['' for x in range(w)] for y in range (h)]
	c = 0
	r = -1
 	
	for child in tree.getiterator():
		tag = child.tag
		text = str(child.text)
		text = str(text).replace(',', ';')
		if not '\n' in text and not '\t' in text:
			#print(tag, text)
			if 'Name' in tag:
			#	print("NAME")
				c += 2
				r = 0
			#print(r, c)
			r += 1
			master[r][c] = tag
			
			c += 1
			master[r][c] = text
			c -= 1
			
	#TODO Add it to the table
	table = event.source.parent.getComponent("PwrTable")
	#event.source.parent.getComponent('PwrTable').data = system.dataset.clearDataset(table.data)
	#master = system.dataset.toDataSet(master)
	#event.source.data = master 
	#sys	tem.db.refresh(table, "PwrTable")
	#table = system.dataset.addColumn(table, scaninfo_label, "Scanned Info Labels", str)
 	
	#Exporting to an CSV file
	#print('\n'.join(['\t'.join([str(cell) for cell in row]) for row in master]))
	
	thisfilename = os.path.split(path)
	thisfilename = thisfilename[1]
	thisfilename = thisfilename.strip('.xml')
	thisfilename = thisfilename + '.csv'
 	
	f = open(os.path.join(os.path.expanduser('~'), 'Documents', thisfilename), 'w')
			
	for r in range(h):
		for c in range(w):
			#if master[r][c] != '':
			f.write(master[r][c]+',')
		f.write("\n")
	f.close()

```

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [May 28, 2019, 3:27pm UTC](https://forum.inductiveautomation.com/t/setting-the-data-set-to-a-2d-matrix/25310/6 "2019-05-28T15:27:20Z")

</div>

So it looks like the variable master is indeed a list of lists, which is just fine. All you’re missing is a list of headers.

Something like

```auto
headers = ['Col ' + str(col) for col in range(w)]

table.data = system.dataset.toDataSet(headers,master)

```

---

<div class="post-metadata">

**Author:** ![ammermanbrittanyf](https://avatars.discourse-cdn.com/v4/letter/a/45deac/32.png) [@ammermanbrittanyf](https://forum.inductiveautomation.com/u/ammermanbrittanyf)\
**Post date:** [May 28, 2019, 3:30pm UTC](https://forum.inductiveautomation.com/t/setting-the-data-set-to-a-2d-matrix/25310/7 "2019-05-28T15:30:51Z")

</div>

Thank you so much for your help!

---

<div class="post-metadata">

**Author:** ![krunallathiya21.kl](https://avatars.discourse-cdn.com/v4/letter/k/e0b2c6/32.png) [@krunallathiya21.kl](https://forum.inductiveautomation.com/u/krunallathiya21.kl)\
**Post date:** [January 1, 2021, 12:11pm UTC](https://forum.inductiveautomation.com/t/setting-the-data-set-to-a-2d-matrix/25310/8 "2021-01-01T12:11:00Z")

</div>

The system.dataset.toDataSet() function converts from a normal DataSet to a PyDataSet, which is a wrapper class that makes working with datasets more Python-esque.

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [January 1, 2021, 7:52pm UTC](https://forum.inductiveautomation.com/t/setting-the-data-set-to-a-2d-matrix/25310/9 "2021-01-01T19:52:45Z")

</div>

> [@krunallathiya21.kl](#):
>
> The system.dataset.toDataSet() function converts...

Firstly, no, toPyDataSet() will do that. And secondly, what does this have to do with anything? I saw your other flagged post on another ancient topic (4yrs old) with another seemingly random reply...
