Sign in to follow this  
Followers 0

Getting data from an Autoit array onto Clipboard

3 posts in this topic

Hi all, I'm pretty new to Autoit and very new to these forums, so please be gentle with me!

I have an autoit script which takes data from a number of text files (produced by a data logger). Example data is given below:








My script then reads ~4000 lines from the left and right channels into separate arrays ($Left and $Right). It then calculates $Left-$Right and $Left +$Right and places these into arrays.

I then use the _ArrayToClip() command to copy the array to the clipboard where I then paste it into excel (using $myobject.ActiveSheet.Paste). I do this for all four arrays and then repeat the whole process for the next text file. There could be up to 80 text files which need to be processed and placed into excel. -Speed is therefore quite important.

The good thing is that the above works.

It looks as though the slowest part is actually pasting each column into excel. What would make things faster is if I could create a multidimensional array (sorry about the terminology here, I have a VB/VBA background) for the data and paste it all in one go. (I could work around the Autoit array size limitations.) My problem is that whenever I try to copy/paste a multidimensional array into excel I get an error. Is there a better way to do this than using _ArrayToClip?

I have also used _ExcelWriteArray() from the EXCELCOM UDF but also get error messages when using multidimensional arrays.


Am I doing it wrong?

Is there a better way to get multidimensional data either into Excel or onto the clipboard?

Are there any examples of code which does this?

My current code is listed below. I appreciate that it can be optimised much more but we all have to start somewhere and will improve the fourier transform bit later.

#Include <File.au3>

#Include <Array.au3>

#include <GUIConstants.au3>

;#include <EXCELCOM UDF.au3>


Dim $aRecords

Dim $szDrive, $szDir, $szFName, $szExt

dim $myobject

dim $excelPID

dim $x

dim $filenumber

dim $left[4099];[17];[$lines-9]

Dim $right[4099];[17];[$lines-9]

dim $LminusR[4099];[17];[$lines-9]

dim $LplusR[4099];[17];[$lines-9]

dim $hFilesFolders

dim $rows


;Automatic version, processes all files in the script directory.

;get the dirve and directory details

$TestPath = _PathSplit(@ScriptFullPath, $szDrive, $szDir, $szFName, $szExt)

$mydrive=$szdrive; these bits are legacy really to feed the correct info into parsefile.

$mydirectory=$szdir; a variable like $path would probably have been better.

;find the files of defined type in the script directory

$FileList=_FileListToArray(@scriptdir & "\","*"& $filesuffix,1)

If @Error=1 Then; check that the file exists

MsgBox (0,"","No Files\Folders Found.")



;make progress bars GUI

GUICreate("Processing Softness Meter Data",420,100,-1,-1,-1)

GUICtrlCreateLabel("Overall Progress",20,10,100)

GUICtrlCreateLabel("File Reading",20,40,100)

GUICtrlCreateLabel("File Writing",20,70,100)

$allfiles = GUICtrlCreateProgress (180,10,200,20,$PBS_SMOOTH)

$singlefile = GUICtrlCreateProgress (180,40,200,20,$PBS_SMOOTH)

$writing = GUICtrlCreateProgress (180,70,200,20,$PBS_SMOOTH)

GUISetState ()

doexcel(1,"");My function to start a copy of excel so that all addins are loaded

; so that a fourier analysis can be run.

for $n=1 to ubound($filelist)-1;this section parses the datafiles

$filename=$filelist[$n] ;i'm sure I have a good reason for this line!

GUICtrlSetData ($writing,0);resets the file-write progress bar

$filepathname=$mydrive & $mydirectory & $filename



If Not _FileReadToArray($filename,$aRecords) Then ;check that the file has been loaded correctly

MsgBox(4096,"Error", " Error reading log to Array error:" & @error)



For $x = 258 to 4354;reads the required rows to build the FT dataset then put into excel

GUICtrlSetData ($singlefile,($x-258)/4096*100);progress bar

$aa=StringRegExp($aRecords[$x],";-{0,1}[0-9]{1,3}.[0-9]{1,7};",1) ;LEFT

$bb=StringRegExp($aRecords[$x],";-{0,1}[0-9]{1,3}.[0-9]{1,7};\z",1) ;RIGHT

for $j=0 to UBound($aa)-1; do the maths then run function to place in excel








_ArrayToClip($left);copy to clipboard

doexcel(2,"Left");##################### put the data into excel ###########









;more excel stuff

doexcel(3,"");do the FT stuff

func doExcel($section,$Channel)

;This function looks after all the excel processing.

;The $Section string dictates which part of the process to run.

Switch $section

case 1;initialiation of the excel sheets

;write directly into an excel sheet.

;make the excel sheet

;check that excel is running

splashtexton("","Loading Excel",120,30,-1,-1,1)


if $excelup=0 Then

;start excel

shellexecute("excel.exe","","","open",@SW_HIDE )

sleep(10000) ;pauses while excel opens



$excelPID="Excel Up Already"


$MyObject = Objget("","Excel.Application"); Open an Excel Object NEEDS EXCEL TO BE OPEN FIRST

If @error Then

MsgBox(0, "Excel Error:", "Error creating Excel object!")



If Not IsObj($MyObject) Then

MsgBox(16, "Excel Error:", "Error creating Excel object!")




$myobject.Application.Calculation = -4135

$myobject.Application.EnableEvents = False

$MyObject.Visible = 0





case 2;writes data into cells


switch $Channel

case "Left"




case "Right"




case "LPR"




case "LMR"





$myobject.activecell.offset(-1,0).value= stringleft($filename,stringlen($filename)-4) ;add the file name to the top of the col

case 3;do FT calculations

;create integrals




$selectionrange="b4098:" & chr(65+ubound($filelist)-2) & "4098"



;get to bottom of the table



$myobject.activecell.offset(-1,1).value="FT OUTPUT"

$myobject.activecell.offset(-1,2).value="Absolute Value"



$myobject.ActiveCell.FormulaR1C1 = "=AVERAGE(R[-4098]C:R[-4098]C["&$n-2 & "])"





$myobject.Application.Calculation = -4105

$myobject.Application.Run ("ATPVBAEN.XLA!Fourier", $myobject.ActiveSheet.Range("$A$4100:$A$8195"),

$myobject.ActiveSheet.Range("$B$4100:$B$8195"), False, False)











;now do the summary table

$myobject.range("G4100").value="FINAL OUTPUT"





$myobject.range("h4101").formula="=average(a4098:" & chr(65+ubound($filelist)-2) & "4098)"

$myobject.range("h4102").formula="=Stdev(a4098:" & chr(65+ubound($filelist)-2) & "4098)"





$myobject.range("i4107").value="Change of integral"

$myobject.range("j4107").value="Error Estimate"




































;copy the block of calculations to the 3 other positions AA BA CA

;then run the FTs for these datasets.












;do the FTs


$myobject.Application.Run ("ATPVBAEN.XLA!Fourier", $myobject.ActiveSheet.Range("$AA$4100:$AA$8195"), $myobject.ActiveSheet.Range("$AB$4100:$AB$8195"), False, False)


$myobject.Application.Run ("ATPVBAEN.XLA!Fourier", $myobject.ActiveSheet.Range("$BA$4100:$BA$8195"), $myobject.ActiveSheet.Range("$BB$4100:$BB$8195"), False, False)


$myobject.Application.Run ("ATPVBAEN.XLA!Fourier", $myobject.ActiveSheet.Range("$CA$4100:$CA$8195"), $myobject.ActiveSheet.Range("$CB$4100:$CB$8195"), False, False)

;rename the sheet


;make visible


$myobject.Application.EnableEvents = True

$MyObject.Visible = 1

$MyObject = 0 ;release excel

;sleep(10000);wait and close

;if $excelPID<>"Excel Up Already"then processclose($excelPID);kills all of excel without saving anything!



Many thanks for reading so far.

Share this post

Link to post
Share on other sites

I have tried unsuccessfully to paste multidimensional arrays into Excel.

Why not load those text files directly into Excel with the OpenText() workbook method, using the colon as a delimiter? You could then relatively quickly make & paste your calculations.

$MyObject.Workbook.OpenText("C:\logfile.txt", Default, Default, Default, Default, Default, False, False, False, False, True, ";")

Give something like that a go.


(Yet Another) ExcelCOM UDF"A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, fight efficiently, die gallantly...[indent]...specialization is for insects." - R. A. Heinlein[/indent]

Share this post

Link to post
Share on other sites

Many thanks for the reply.

I was going to do it all in Excel VBA as this would have had a shallower learning curve, however there is an issue that the text files can be very very large, enough to knock out excel. Though this is currently not a problem but we are getting close, if I work to that limitation, my colleagues will need it exceeded!

Also the preprocessing is likely to be used in another analysis technique, so if I do the calculations within autoit, I can fork the output to whatever technique I need to run.

Actually with one or two tweaks I am getting the code quite efficient.

I have an idea that instead of making the array multidimensional, I keep it in a single dimension and add tabs (or commas etc) to delimit it:

$myarray[$x]=$myarray[$x] & chr(9) & $MyNextField

If I paste that into Excel, I should be able to convert text to columns.

What do you think? Will it work?

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  
Followers 0