LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Import CSV File, is this the best way?

I am developing a batching process that imports a CSV file and uses it as the recipe.  I am fairly new to labview but I managed to get the attached VI to work after cutting and pasting from a few VI's.  Is there a better way to accomplish this and what is the best way to access the individual ingredients in my sequence?
Download All
0 Kudos
Message 1 of 9
(3,995 Views)
As it is the VI does not handle more than one line, so this approach won't work. However, all you really need to do is to change the delimiter at the "SpreadSheet String to Array" to a newline character so you get all the lines. Then, you need to feed that array into a for-loop that does what you're doing at the bottom for each line. You will probably want to package all that up into a nice bundle so you get an array of clusters so you can easily access your elements. See attached modification.

If you can establish some rules regarding the line format you may be able to get away with using the "Scan From String" function.
0 Kudos
Message 2 of 9
(3,967 Views)
I am trying to do a similar thing. Using csv files for recipes. I am stuck on one aspect. I want to have a sub-Vi that will allow for editing the recipe file. I am using the spreadsheet-to-array vi and then displaying the result in an array indicator. I would like to allow the user to edit the values in the array and then save it to a file, but since the array is presented in an indicator, how do I "collect" the changes made to the text, so I can get them saved to a file? Probably simple, but I am a newbie.
0 Kudos
Message 3 of 9
(3,955 Views)
The simplest way is to use a local variable. Set your array to be a control and when you do a file read, wire the output to a local variable of the array. Here's a simple example with Read from Spreadsheet File and Write to Spreadsheet File. Both can be easuky modifed to read/write strings by the way.
0 Kudos
Message 4 of 9
(3,945 Views)

Smercurio_fc

Thanks for responding but I don't understand what your mod does that my original VI didn't.  When I use a comma as a delimiter, all of the individual ingredients are imported and it works, when it is changed to a new line character then yours works, what is the difference?  The recipe always has the same # of fields and is only required once at the start of the batch, and is always the same format (%s,%s,%s,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%s).  How do I access the individual ingredients in your output array?  Sorry if this seems elementary!

0 Kudos
Message 5 of 9
(3,931 Views)
My comments were based on the assumption that your CSV file would have more than one line of text, as CSV files typically do. If your CSV file only has one line it seems like an awful waste. As I stated, if you have more than one line of text, which is my assumption for having more than one recipe, then your VI won't work.

As for access if this is a subVI that outputs all the recipes (the array of clusters), then downstream you just need to index out which recipe you want and then use an unbundle function.

As an alternative, you can index out at the initial array of strings that correspond to the lines in the file for the specific line that you want. Then, you can use your code (though you can replace the "Array to Cluster" and "Unbundle" with a single "Index Array" as I was doing inside the loop) and output a single cluster.

Perhaps it may help if you provide a better idea of what you're doing with the data, as you've only provided a snippet of what you're doing, and there's no "best way" to do this since it all depends on your application.
0 Kudos
Message 6 of 9
(3,924 Views)
As a follow-up, I forgot to mention that you can use the "Scan From String" function as shown in attached VI.

This assumes that the only field that can have spaces is the second field. This is reflected in the scan string.
0 Kudos
Message 7 of 9
(3,920 Views)
The application is a batching sequence for a concrete plant.  The recipe is created in another application and then imported into labview as a csv file.  The format and # of fields is the same all the time, only the values of the ingredients change.  So as an example, the first 3 strings are just for information to the batch operator, the first numeric is quantity of meters of concrete, the next numerics are the actual ingredients.  I need to access all of the rest of these fields in my sequence.  I have attached a very basic VI showing a small portion of what will be the batch VI, I hope this helps.
0 Kudos
Message 8 of 9
(3,916 Views)
Unfortunately that VI doesn't really tell me anything since it seems to have no relation to your reading of the recipe. The key here is whether or not this file will contain more than one line. You've never addressed this question, even though I've stated it numerous times. However, your statement "The recipe is created in another application and then imported into labview as a csv file" seems to imply that the file will contain only one line. If that's the case, you can simplify the code to get your recipe in a nice little cluster thusly:


Message Edited by smercurio_fc on 04-20-2006 08:58 AM

0 Kudos
Message 9 of 9
(3,903 Views)