Importing from Excel; .csv questions

mr_johnson22's icon

mr_johnson22

5月 12 2011 | 11:44 午後

Right now I'm trying to grab data from an Excel spreadsheet and import them into a Max patcher. Basically, I want to be able to grab a number (the data will always be number values, never text) from any given cell in the spreadsheet, and put that number into Max.

After searching through the forums here, I know that I can save an Excel file as a .csv file for Max to read as text (https://cycling74.com/forums/midi-from-excel). But after saving my .csv file, I don't know what to do with it! Do I write the file into a coll object or something?

Sorry if this is a basic question...I'm rather new at Max.

Justin G's icon

Justin G

5月 13 2011 | 7:19 午前

Max Patch
Copy patch and select New From Clipboard in Max.

hey i'm guessing you probably want this in a [coll]? its possible to load data from a excel csv into max coll but not directly. coll wants: each line to begin with a int followed by a comma, fields seperated by a space (not comma), and lines ending with a semicolon. [text] is pretty indifferent to the contents though so you could just load the csv into that with (read) after you add a '.txt' extensiom to the file name. but if you want it in a coll, see attached. i made this to work with text and add sequential numbers as each line enters the coll but it would be fine with numeric data. this patch doesn't verify the integrity of the csv in any way. empty fields cant be blank (just fill with a '0' or something in excel). other things would probably break it tool. this is more of a job for java. i hesitate to post this but, eh:

2246.driftpattern.png
png
mr_johnson22's icon

mr_johnson22

5月 14 2011 | 2:10 午前

First off: thank you so much!!! This will DEFINITELY help, and there is no chance that I ever would have thought this up.

Secondly: this might complicate matters, but is there a way to do this without manually saving the Excel file...? The reason for this is that the Excel file will constantly be updated by means of a DDE server, so the values to be imported are always changing in real-time, and having to manually save the Excel as a .csv takes away that real-time aspect of it.

The data I'll be using is being received by one program (MT4), and from there it's sent to Excel in real-time, which is to be sent to Max also in real-time, if possible. So the next best thing I can think of off the top of my head would be to set Excel's auto-saving to the shortest time interval possible and use the patcher you just provided. But if there's a better way to preserve real-time values, that would be a big help.

And again, sorry if this is basic! :(

PS: Does Max support DDE servers? That would be kinda nice...

Justin G's icon

Justin G

5月 15 2011 | 9:27 午後

see attached excel file. whatever is on sheet1 will be saved as "data4max.csv" every 13 seconds. the old data is overwritten. press ctrl+t to start the macro. to see the vba code press alt+f11 to open the editor. to change the time interval, double-click "Module1" and find the number 13. so maybe put your dde stuff on sheet2 then format for the record to go to max on sheet1.

to get [text] to read .csv without renaming the file, goto your max "init" folder, open "max-fileformats.txt" and add a line like "max fileformat .csv TEXT 0 "CSV Text file" textfile;"

add [filewatch] to the patch. it sends a bang every time the contents of the file has changed (see help). with the max example above, instead of indexing the data with that auto incrementing number (the second pair of [i] below the second uzi) maybe concatenate some things in excel to make a big timestamp int to send the coll for index. to make the index stop resetting to 1, delete the green patch cord going to the lower [i 1].

2252.driftpattern.xls
xls
mr_johnson22's icon

mr_johnson22

5月 16 2011 | 5:29 午前

Oh wow! Thanks!! I'm going to try this out tomorrow!

mr_johnson22's icon

mr_johnson22

5月 17 2011 | 1:58 午前

I think that was the ticket!!! But I can't get too excited yet...I can't get floats inputted into [text] to not be cut off after the 3rd decimal point! And I do need to work with values with 4 decimal points...arghhh

For instance, if I have an Excel cell with a value of 0.12345, the value that will end up in the second [text] and ultimately the [coll] is 0.123. Long strings don't get cut off, though.

Is this an inherent issue with [text]? Because I tried connecting a flonum to a [text], and inputting numbers with 4 or 5 decimals, and only the first 3 are ever shown inside the [text].

If this is a limitation in [text] I won't be able to work around, I'll see what I can do to compensate. Perhaps if I multiply floating points by a large multiple of ten before putting it in the second [text], and dividing it back to normal before using the value, I can get past this.

But is there a floating point precision tricks of [text] that I'm not aware of? That's all I will ask of you now...you've done a lot already OTL

mr_johnson22's icon

mr_johnson22

5月 17 2011 | 2:28 午前

Actually, never mind! The multiplication thing worked!

*Now* I can say YIPPEE!!!!! :D

Thank you SO very much. Never would I have been able to do this without your help. :)

sampointone's icon

sampointone

6月 26 2014 | 5:30 午後

Hi Mr Johnson - i found this thread this morning and am also having issues with the text object and decimal points. Within your experience how did you solve this? My data is coming from long lat co-oodinates which come as 51.4930794 / -0.0540378. Don't know if you solved this but I am using the same (Justin G 'hats off') example for packing coll with a converted .cvs file. I eventually want to map this data to between 0.-1. I've attached data as .cvs incase your interested. :) many thanks in advance.

ollyboyd's icon

ollyboyd

10月 08 2014 | 2:24 午後

Hi Sampointone, I believe mr Johnson multiplied his number in excel (in your case multiply by 10,000) then once the number arrives in Maxmsp, divide it by 10,000 to return it to normal.

Ursine's icon

Ursine

7月 22 2015 | 7:05 午後

Howdy Justin G,
Just made an account to thank you for this patch from 4 years ago. It really helped in a pinch!
~John

Rob Chafer's icon

Rob Chafer

4月 28 2017 | 2:36 午後

Likewise now 6 years on!! thanks :) really useful Justin G

Rob

MJ's icon

MJ

4月 09 2018 | 12:16 午後

and again in 2018 ...
thanks

mj

Uchida_Lab's icon

Uchida_Lab

1月 11 2019 | 2:10 午後

And in 2019. Thank you.

Peter Redecopp's icon

Peter Redecopp

3月 20 2019 | 6:53 午後

haha, I too am sending thanks to Justin G in 2019. Works great!

Sam Risser's icon

Sam Risser

11月 06 2019 | 4:58 午前

Justin G, you have yet another satisfied customer. Works like a charm!

Max Quinones Santander's icon

Max Quinones Santander

5月 05 2021 | 7:50 午前

2021 Still Working =)

sousastep's icon

sousastep

12月 24 2021 | 7:02 午後

Many thanks. I used this for my LED sousa bell.

chewiesmissus's icon

chewiesmissus

1月 15 2022 | 10:16 午後

2022 checking in!

I had a problem with the Excel sheet not saving - I am on a Mac and had to change a back slash to a forward slash on module 2 in Excel.

In this line:
ActiveWorkbook.SaveAs FileName:=ThisWorkbook.Path & "\data4max.csv",

Changed to:
ActiveWorkbook.SaveAs FileName:=ThisWorkbook.Path & "/data4max.csv",

I also created a data4max.cvs file in the same folder that was then successfully being overwritten :)

Other than that so so helpful! :) Big thanks!

MJ's icon

MJ

8月 06 2024 | 1:48 午後

and in 2024 . again

it already helped me in 2018 (see above) .

forgot about this and managed to get back here (and see my own TY post ...)

-mj