Building an array from a for loop.

Hi all
I have a vi which reads from a file and manipulates the data using for loop. I am taking each element and multiplying it with a constant and converting it to I32. I would like to build back to a new array with the new values. I have attached the vi and a sample data file to read from.
Please suggest if there are any better ways to do this.
Thank you. 
Solved!
Go to Solution.
Attachments:
file_read.vi ‏11 KB
file_read.txt ‏1 KB

You can multiply the array with the constant and use the array instead of multiplying it with each element.
The best solution is the one you find it by yourself

Similar Messages

  • How to output last 4 arrays from a For Loop?

    Hi People,
    I am almost at the end of my tether. I really hope someone could please help me.
    Any input would be welcome. VI attached.
    VI explanation:
    I initialize an array.
    The random number generator simulates my camera input.
    Depending on which iteration it is, the data is added into one of four output arrays.
    'x-y*floor(x/y)' gives a remainder value of 0, 1, 2 or 3.
    'Case Selector' just adds 1 to 'x-y*floor(x/y)' to get case 1, 2, 3 or 4.
    Shots 1, 5, 9 etc are added to Array 1 (Case 1).
    Shots 2, 6, 10 etc are added to Array 2 (Case 2).
    Shots 3, 7, 11 etc are added to Array 3 (Case 3).
    Shots 4, 8, 12 etc are added to Array 4 (Case 4).
    Averaged Output displays the averaged value i.e. the total value divided by the number of shots saved in that array ('Set (IQ+1)').
    My problems are:
    1) I would like to output only the last 4 sets of data from the Averaged Output 1, 2, 3 and 4 to 4 different files (i.e. save as .csv what is displayed onscreen in indicators 'Averaged Output 1', 2, 3 and 4 at the end of all iterations.
    Where should I place my file save diagram disable so that it does this?
    Putting it outside the main For Loop with auto-indexing on gives me one file with all previous data. (This is not feasible as my number of shots should number in the thousands)
    Putting it outside the main For Loop with auto-indexing off gives me only the last set of data. (I need the output for 4 Arrays, not just the last run)
    Putting it inside the main For Loop (as shown) gives me the same number of files as number of iterations. (Again not feasible due to large number of files that will be generated and slow camera capture)
    For the sake of fast camera capture, I would like these 4 files to only output once all the image captures are complete.
    2) Would preferably like to name the file once and for the programme to append '(1)', '(2)', '(3)' and '(4)' to filename of the appropriate 'Averaged Output' Arrays but file path controls are another big headache for me.
    3) P.s. is initializing one array enough to avoid using Memory Manager? Or should I initialize 4 arrays?
    I am using Labview 2010.
    Thanks so much,
    Jaslyn
    Solved!
    Go to Solution.
    Attachments:
    Help Understanding Arrays and file paths (10).vi ‏26 KB

    Thanks so much Lennard!!
    Hi JKSH,
    I've been busy trying to complete the dang code, but for completeness so that other users can learn too, I shall answer your queries. Thanks for looking in.
    First, what do you mean by "add"? Your code in your case strcuture sums your input values, so your array size doesn't change. However, you also said "Putting it outside the main For Loop with auto-indexing on gives me one file with all previous data. (This is not feasible as my number of shots should number in the thousands)" -- it sounds like you are expecting an array with thousands of elements. So, what should "Output Array N" look like?
    Ans: Output Array N should be a 4x4 array of the summation of all the numbers generated during every 4th iteration.
    i.e. if random number 1=1, random number 2=2, etc, And 'No of iterations' is 12,
    Output Array 1 should be 1+5+9:
    15 15 15 15
    15 15 15 15
    15 15 15 15
    15 15 15 15
    (Note: Most of your arrays are 4x4, but in Case #2 you created 128x128. I prersume this is a typo?)
    Ans: Kind of, I'm using a 4x4 array of randomly generated numbers as an example but my actual data will be the 128x128 pixel output of a camera. And during actual experimentation, 'No of Iterations' will number in the thousands.
    Second, why do you add your "simulated camera input" to the "Initialized Array"? You can add it direcly to the previous output inside your case structure.
    Ans: See answer to question 3
    Third, have a look at shift registers: http://www.ni.com/gettingstarted/labviewbasics/shiftregisters.htm. Use them instead of Feedback Nodes to make your code tidier.
    Ans: I did try.. But I can't seem to add shift registers to the case structure, only the for loops outside. Are you sure it can be done..?
    jaslyn wrote:
    1) I would like to output only the last 4 sets of data from the Averaged Output 1, 2, 3 and 4 to 4 different files (i.e. save as .csv what is displayed onscreen in indicators 'Averaged Output 1', 2, 3 and 4 at the end of all iterations.
    Where should I place my file save diagram disable so that it does this?
    For the sake of fast camera capture, I would like these 4 files to only output once all the image captures are complete.
    You will need to call "Write to Text File.vi" 4 times to write 4 files. So, you should finish your loop, then call this VI 4 times.
    Ans: How do I get it to extract the data from each of the 4 cases in my case structure?
    jaslyn wrote:
    2) Would preferably like to name the file once and for the programme to append '(1)', '(2)', '(3)' and '(4)' to filename of the appropriate 'Averaged Output' Arrays but file path controls are another big headache for me.
    You can create strings first, then convert them into paths: http://zone.ni.com/reference/en-XX/help/371361G-01/glang/string_to_path/
    Thanks
    jaslyn wrote:
    3) P.s. is initializing one array enough to avoid using Memory Manager? Or should I initialize 4 arrays?
    I'm not sure what you're asking; can you please clarify what you mean by "avoid using Memory Manager"? But anyway, you've actually initialized FIVE arrays: 1 outside the loop, and 1 inside each case.
    I've read that to avoid fluctuations in memory usage, it is a good idea to initialize arrays to their expected size before the start of data collection. That was just what I was trying to do.

  • Methods to build an array inside a For Loop.

    Let me introduce to you my nested For Loops:  
    They output a 2D boolean array depending on two 1D boolean arrays of different lengths.  This is all well and good however, in another part of my program, I want this to be a 1D array instead.
    Idea #1:
    "I'll just reshape the array at the end. Simples!"
    Idea #2:
    "Hang on, why am I using three primitives outside the loop when I can use just one inside it along with a shift register?"
    Idea #2.5:
    "Oh, wait... I remember reading somewhere about continuously building an array (with concatenate inputs) inside a loop being a bad thing and quite slow. Let me test this."
    Spoiler (Highlight to read)
    Idea #3:
    "Umm... okay, so actually the second way is faster.  I think if I initialize the shift register first then replace subsets of the array, that might also be better."
    Spoiler (Highlight to read)
    Idea #4:
    "Yep - seems much better. (Something to do with LabVIEW not having to constantly allocate more memory on every iteration of the loop)  Looking at this closer, I don't actually need the inner For Loop because of the way I'm handling the arrays.  Try again...!"
    Spoiler (Highlight to read)
    Conclusion(?) #1:
    "Well I seem to have arrived at something that looks messier, but executes about 6x faster than my initial idea. That said, the difference rapidly increases as the input arrays get much bigger (becoming orders of magnitude different)."
    Is this the most efficient way of doing what I want?  Or is there something even better (and works in LV 8.5)?
    Never say "Oops." Always say "Ah, interesting!"

    Thank you both for the feedback.  It never occurred for me to recheck the logic after I'd initially extracted the nested For Loops, but the AND gate output is obviously the same now that it's been pointed out!
    Continuing on with Idea #4, I replaced the Select with an AND then compared the times.  (also meant I didn't have to initialize one of the arrays anymore)
    Spoiler (Highlight to read)
    It appears that the AND gate was slower but then I tried with different sizes of initial arrays and in one case, the timings were 'reversed'.
    This only happened when the top array (the one that is fed into the AND gate whole) was reduced to a length in the region of 5 elements long.
    Spoiler (Highlight to read)
    Why does this happen?  Is it something to do with the AND gate possibly being optimised for single element operations so smaller arrays are handled better...?
    Never say "Oops." Always say "Ah, interesting!"

  • Truncating an array within a for loop

    I'm currently working with a program that builds an array of 3000*N real numbers, where N is a control that I specify. I want to pass this array into a for loop that runs N times, each time splitting the first 3000 numbers of the array. So, in other words I want the for loop to execute enough times such that the initial array will be completely divided into groups of 3000 when I'm finished. Later I'll be finding the maximum value of all of these subarrays and taking the average of those. What I tried first is to pass the array into the for loop, then truncate it at index = 3000, but the problem is that I can't hard code the loop to do this N times. Any advice or other methods you'd recommend for doing this?
    Thanks.
    Message Edited by Robtn on 03-05-2010 03:17 PM

    Mark--I don't think I can use that approach. Let me be more specific on what the problem is:
    Our "big program" is a giant while loop that constantly reads in voltage values from a DAQ assistant.
    It has two modes, the first of which I'm having a lot of trouble with. This mode uses a for loop (N = 3000) nested within the giant while loop.
    When the for loop activates, the DAQ assistant takes reads 300 samples for every iteration and takes the maximum of those 300 values. It then builds an array composed of all the maxima. After the for loop has executed 3000 times, a case structure activates, which then finds the maximum of the array of maxima.
    I want this (N=3000) for loop to execute an arbitrary number of times, a number that I will specify. Each of those times it should generate a completely seperate array of maxima. So, if I want the for loop to run 5 times, I want 5 different arrays.
    My approach for doing this was to change N=3000 to say N = 15000, then split up the resulting array 5 times. So, I need an entirely separate for loop that reads in this array of 15000 maxima on its own and splits it 5 ways. I think what you're suggesting is to use a for loop outside of the N=3000 loop, which is a good idea in theory, but experience has shown that this will not generate 5 separate arrays. It will actually concatenate each array into one, hence the problem we currently have.
    Yik--I won't have access to Labview over the weekend. Is it possible to take a screenshot and post it, or maybe a short description?
    Thank you for the responses.

  • Building 2D array in a while loop

    I'm trying to build an array from hex data received from a serial device(using VISA) inside a while loop.  Can I take the hex string(262 bytes long) and write it into the first element of an array?  Everytime the loop is executed the next hex string  I would like to store  it into the next element of the array. Can someone give me some hints on how to get started or a basic vi that outlines building an array of this type in a while loop.  Thanks. 

    Where is the 2D aspect of this? What you're describing sounds like a 1D array, and you can just use auto-indexing to do this.
    To learn more about LabVIEW it is recommended that you go through the tutorial(s) and look over the material in the NI Developer Zone's Learning Center which provides links to other materials and other tutorials. You can also take the online courses for free.
    Message Edited by smercurio_fc on 04-11-2008 04:07 PM
    Attachments:
    Example_VI_BD2.png ‏2 KB

  • Transposing data in an array using a For Loop

    I am trying to transpose some data in an array using a  for loop starting from the size of the array then decrement down to 0. Can someone please help if you have an example?
    Solved!
    Go to Solution.

    I feel as if reverse 1D array may help
    Edit: misunderstood what your second post meant.
    CLA, LabVIEW Versions 2010-2013

  • Creating an array in a 'for' loop (resetting loop count)

    Hi,
    I am trying to integrate an acceleration signal coming from a CRIO analogue input to obtain the speed and then further integrate to get the displacement of a vibration system.
    I understand that in order to perform the integration, an array has to be fed in. My problem is that I could not buffer the signal in a buffer block as I couldn't find any buffer blocks. so I decided to create my an array of data using a 'for' loop. The 'for' loop nests inside a timed loop. The problem that I got stuck at is how to reset the 'for' loop in order to refill the buffer again at every iteration of the outer timed loop.
    I have attached the code if that helps. Note that I am using labview 2013.
    I would be gratefull if someone who had encountered a similar issue before can help me.
    Regards
    Ali
    Solved!
    Go to Solution.
    Attachments:
    acc_test.vi ‏42 KB

    Dear Altenbach,
    Many thanks for your reply.
    I need to make it clear that I have not had enough hands-on with labview, I have only used it for sometime in the past and now I have to in order to perform some active damping on a 50Hz running machine. I must stress the fact that what i find more intuitive to do in a different programming language (matlab or C) is not as straightforward in labview. so please bear with me if my code has basic mistakes .
    All what i am trying to do is obtain the speed and position from an acclerometer signal. unfortunately i dont have a position sensor so i have to measure acceleration then perform time integrations in order to obtain the speed and position respectively. I am using a compact Rio (later in FPGA mode) to take physical measurements of acceleration from the vibratory machine and where i managed to do this bit successfully. when it came to performing the integration, i had to create an array inside my timed loop since i couldn't find a buffer block. I implemented a 'for' loop inside a timed loop in order to build the array. however i got stuck at resetting the count of the 'for' loop in order to refill the array with new values at every new iteration of the outer timed loop. unfortunately, even a sensible answer has been suggested before, i dont seem to have been capable of doing it yet. I really spent all my day trying to figure a way out but not achieved it yet, nevertheless I have learnt many many things by playing around.
    I would be gratefull if you could comment on a possible way of doing what i am trying to do.
    Kind Regards
    Ali

  • Passing an array in a for loop to a procedure

    I am trying to pass an array in a cursor for loop to a procedure which performs a table insert using the array's contents. Somehow I am missing something, or it is not possible. The compile error states: PLS-00306: wrong number or types in call to 'insert_address' I checked to be sure I am creating the arrays in both cases from similar data objects. Both address and work_address_table contain the same 4 columns with the same data types.
    create or replace package work_address as
    FUNCTION populate_address return boolean;
    procedure insert_address(in_address IN work_address_table%ROWTYPE);
    end work_address;
    create or replace package body work_address as
    function populate_address return boolean is
    cursor c1 is
    select 'H' as header,
    street1 as street
    city as city,
    NULL as state
    from address
    where city = 'HANOVER';
    TYPE addressT IS TABLE OF c1%ROWTYPE INDEX BY BINARY_INTEGER;
    rec1 addressT;
    BEGIN
    OPEN c1;
    FETCH c1 BULK COLLECT INTO rec1 LIMIT 500;
    FOR i IN 1..rec1.count LOOP
    rec1(i).state := 'US'
    insert_address(rec1(i));
    exit when c1%notfound;
    END LOOP;
    CLOSE c1;
    return TRUE;
    END populate_address;
    PROCEDURE insert_address(in_address IN work_address_table%ROWTYPE) IS
    BEGIN
    INSERT INTO work_address_table
    VALUES (in_address.header,
    in_address.street,
    in_address.city,
    in_address.state);
    COMMIT;
    END insert_address;
    END work address;
    /

    Both address and work_address_table contain the same 4 columns with the same data types.Are you 100% sure about this?
    SQL> declare
      cursor c1
      is
        select 1 deptno, dummy dname, 'Loc' location from dual;
      type addresst is table of c1%rowtype
        index by binary_integer;
      rec1   addresst;
      procedure p (d dept%rowtype)
      as
      begin
        dbms_output.put_line(d.dname);
      end p;
    begin
      rec1 (1).dname := 'z';
      p (rec1 (1));
    end;
    z
    PL/SQL procedure successfully completed.but changing just the first column of the cursor:
    SQL> declare
      cursor c1
      is
        select 'xy' deptno, dummy dname, 'Loc' location from dual;
      type addresst is table of c1%rowtype
        index by binary_integer;
      rec1   addresst;
      procedure p (d dept%rowtype)
      as
      begin
        dbms_output.put_line(d.dname);
      end p;
    begin
      rec1 (1).dname := 'z';
      p (rec1 (1));
    end;
    Error at line 3
    ORA-06550: line 20, column 3:
    PLS-00306: wrong number or types of arguments in call to 'P'
    ORA-06550: line 20, column 3:
    PL/SQL: Statement ignored

  • Problem cycling through elements of an array using a for loop ~ error 200609?

    Hi,
    I'm outputting a 194-element array from a MATLAB script node into a for loop where the array is indexed then converted into a 8-bit boolean pattern which I'm trying to output using DAQ Assistant. I'm using LabVIEW 8.2 and a PCI 6133 DAQ using a counter(Ctr0InternalOutput) to time the output. The first element of the array goes through the loop perfectly however never makes it's way back through and throws error 200609: Generation cannot be started because the slescted buffer size is too small. Increase the buffer size. After probing the line before DAQ Assistant I confirmed only one element makes it through the loop and then throws this error? I've trying doing the same things with lower lowel DAQmx vi's using a DAQmx configure output buffer vi to override this buffer size problem, but still get the same result(not the error but same data flow problem)? Anybody know what's going on? I've included the vi's I've been working with. Thanks!
    Millie
    Attachments:
    Untitled 22.vi ‏228 KB
    Untitled 34.vi ‏180 KB

    Hi,
    I'm outputting a 194-element array from a MATLAB script node into a for loop where the array is indexed then converted into a 8-bit boolean pattern which I'm trying to output using DAQ Assistant. I'm using LabVIEW 8.2 and a PCI 6133 DAQ using a counter(Ctr0InternalOutput) to time the output. The first element of the array goes through the loop perfectly however never makes it's way back through and throws error 200609: Generation cannot be started because the slescted buffer size is too small. Increase the buffer size. After probing the line before DAQ Assistant I confirmed only one element makes it through the loop and then throws this error? I've trying doing the same things with lower lowel DAQmx vi's using a DAQmx configure output buffer vi to override this buffer size problem, but still get the same result(not the error but same data flow problem)? Anybody know what's going on? I've included the vi's I've been working with. Thanks!
    Millie
    Attachments:
    Untitled 22.vi ‏228 KB
    Untitled 34.vi ‏180 KB

  • How to traverse an array with a for-loop in this certain way...

    so I need to traverse an array starting from the first element, then from the last element, then the second element, then the second last element, then third element and so on... in a for loop
    how would that look in code?
    Any help would be appreciated, thanks!
    Edited by: inspiredone on Apr 4, 2008 6:34 AM

    Wow, that's a really clever and elegant way to do it promethuzz. I actually came up with something already before I read your post so yeah don't worry about me copying or anything. Mine is not as elegant/efficient and I definitely like yours a lot more but I will just share it anyway...it looks pretty similar to the second algorithm The_Matrix has provided. I figured this out from thinking how quicksort compares its pivot with the two indexes...
        int front=0;
        int back = a.length - 1;
        int counter =0;
        boolean switchflag = true;
        while(front<=back)
          System.out.println(a[counter]);
          if(switchflag)
            counter = back;
            front++;
            switchflag = false;
          else
            counter = front;
            back--;
            switchflag = true;
        } Anyways, thanks a lot for you guys help, promethuzz, The_Matrix, and newark.
    Edited by: inspiredone on Apr 4, 2008 11:25 AM
    Edited by: inspiredone on Apr 4, 2008 11:27 AM

  • Issue with building an array from a cfhttp request result.

    Here is what I am trying to do. Retrieve a bunch of results from a  REST request. Run a query to see if I should be excluding any of the xmltext entries coming back from the rest request. Build an array of the REST xmltext entries except the entries in the cfquery.
    I have it all workign except building the array minus the entries that came back in the cfquery. Here is my code so far.
    <cfquery name="getqueue" datasource="#application.settings.dsn#">
    select * from friends
    where Deactivatedate < #DATEADD('d', 1, CreateODBCDateTime(now()))#
    </cfquery>
    <cfoutput query = "getqueue">
    <cfhttp  blah blah>
    <cfset nodes_parse = XmlParse(CFHTTP.FileContent)>
    <cfset Nodes = xmlSearch(nodes_parse,'friends/friend/date/activedate/')>
    <cfset roleArray = ArrayNew(1)>
    <cfloop from="1" to="#arraylen(Nodes)#" index="i">
       <cfset NodeXML = xmlparse(Nodes[i])>
    <cfset ArrayAppend(roleArray, '[sel_members][]=#NodeXML.activedate.xmlText#&')>
    </cfloop>
    </cfoutput>
    My issue is down in the loop where I do the arrayappend. How would I build an array of values coming back from the cfhttp request but not include any of them if they match up with anything coming back from the getqueue query?

    What about the obvious? Namely,
    <cfif value_from_getqueue IS NOT value_from_cfhttp_request>
    <cfset arrayAppend()>
    </cfif>

  • Why are date and time strings lost when indexing an array in a for loop

    Hi, 
    I have an application where i'm reformatting data from a spreadsheet for graphical display on a LabVIEW dashboard.
    the original spreadsheet has date and time values in separate columns, and i'm merging them and converting to a timestamp value.  But something's not working.  Does anyone know why the string value is lost when the array indexes into this for loop?
    Attachments:
    failed array index.png ‏142 KB

    Can you attach your VI with some typical values? (e.g. create in indicator on the 2D array, run your VI, then turn the indicator (now containing data) into a diagram constant). Place the FOR loop related code and that diagram constant into a new VI and attach it here.
    How many times does the FOR loop run? Could it be that the last element of each 1D array is an empty string? (unless you put a wait inside the FOR loop, you'll never see the other elements in the probe)
    LabVIEW Champion . Do more with less code and in less time .

  • Help on changing array elements with For loop

    Hi,
    I'm wondering if anyone can help me with this problem.  I'd like to be able to modify elements of an array with For loop.  In the attached VI, my goal is to have an array of [0 1 2 3 4 5], but instead what I've got is [2 2 2 2 4 5].  I initialized the array with a value of 2 to help me identify which elements are changing.  Can anyone help me pointing where my mistake is?  Thanks.
    Peter
    Attachments:
    Array test.vi ‏19 KB

    It's a classic issue for LV newbies. In this case you HAVE to use a
    shift register otherwise the modifications of previous cicles are lost.
    It's all easier than it's seems, see attached vi.
    bye,
    manga
    Attachments:
    Array test 2.vi ‏19 KB

  • Iterating through every other (or specified number) of element in an array using a for loop

    I am trying to go through an array but instead of autoindexing I want to skip every other element or maybe a number of elements that will be specified for the user.
    Basic functionality that I want to acheive is the following in C code.
    int temp_var = 10;
    int i=0;
    for(i=0; i<=1000; i=i+temp_var)
    I am very new to LabVIEW and I did not know how to use internal variables.
    Can this be acheived using shift registers?
    I am attaching the code I currently have for this. Let me know what you think.
    Thanks guys.
    Solved!
    Go to Solution.
    Attachments:
    24_Sep_NchanNSamp.vi ‏44 KB

    tbob wrote:
    I have been asking for a new For Loop style that would allow us to specify start, stop, and step conditions, but it seems that many people are just not for this idea.  Don't know why?
    See here.
    I wouldn't say there aren't many people for it.  The idea you linked to was one of a half dozen of basically duplicate ideas, including another one of your own.
    These two ideas have a fairly large number of kudoes showing people want the idea.
    Smart Iterators with Loops
    For Loop Increment

  • I'm creating an array using a for loop, how do I get the values outside the loop while it is running? thanks

    I'm creating a set of values using a for loop and a shift register inside the loop. I want access to these values outside the loop as they are being created inside. When I connect a wire from the shift register to a display outside, it doesn't work. How do I do this? Thank you.
    Attachments:
    tamko_new.vi ‏29 KB

    I tried to create the local variable that was wired to numeric indicator inside the loop. If I try to connect this to the analog output outside the loop, the loop just starts blinking and nothing happens, am I doing something wrong? Thanks again.
    Attachments:
    tamko_new2.vi ‏29 KB

Maybe you are looking for