Importing from Excel; .csv questions
May 12 2011 | 11:44 pm
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.
- May 13 2011 | 7:19 amhey 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:
- May 14 2011 | 2:10 amFirst 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...
- May 15 2011 | 9:27 pmsee 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].
- May 16 2011 | 5:29 amOh wow! Thanks!! I'm going to try this out tomorrow!
- May 17 2011 | 1:58 amI 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...arghhhFor 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
- May 17 2011 | 2:28 amActually, never mind! The multiplication thing worked!*Now* I can say YIPPEE!!!!! :DThank you SO very much. Never would I have been able to do this without your help. :)
- Jun 26 2014 | 5:30 pmHi 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.
- Oct 08 2014 | 2:24 pmHi 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.
- Jul 22 2015 | 7:05 pmHowdy Justin G, Just made an account to thank you for this patch from 4 years ago. It really helped in a pinch! ~John
- Apr 28 2017 | 2:36 pmLikewise now 6 years on!! thanks :) really useful Justin GRob
- Apr 09 2018 | 12:16 pmand again in 2018 ... thanksmj
- Jan 11 2019 | 2:10 pmAnd in 2019. Thank you.
- Mar 20 2019 | 6:53 pmhaha, I too am sending thanks to Justin G in 2019. Works great!
- Nov 06 2019 | 4:58 amJustin G, you have yet another satisfied customer. Works like a charm!
- May 05 2021 | 7:50 am2021 Still Working =)
- Dec 24 2021 | 7:02 pm
- Jan 15 2022 | 10:16 pm2022 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!