LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

switch element at given index in a text file

Solved!
Go to solution

I'm having some difficulties here...

 

What I want to do is to replace/switch element at a given index in a existing text file. When I read data from the text file I use "read from spreadsheet file.vi" and read the elements as strings, so my idea is to use the "write to spreadsheet file.vi" when I want to update the text file.

 

For an example, a text file with elements; 1 1 1

                                                                           2 2 2

                                                                           3 3 3

 

An example of what I want to do is to switch places of the first 1 at index (0,0) and the last 3 at index (2,2), which should give me the result in the same text file; 3 1 1

                                                             2 2 2

                                                             3 3 1

 

Anyone who got any tips or ideas? Thanks..

0 Kudos
Message 1 of 6
(2,142 Views)

You may perform following operations:


1. Read data from file.

2. Convert it to 2D array (if not already reading data as 2D array).

3. Change the 2D data to 1D array (use Reshape Array function).

Eg. [1,2,3; 4,5,6; 7,8,9] to [1,2,3,4,5,6,7,8,9]

4. Perform so called SWITCH operation (use Rotate 1D array function.)

5. Convert 1D array to 2D array (use Reshape Array function).


I am not allergic to Kudos, in fact I love Kudos.

 Make your LabVIEW experience more CONVENIENT.


0 Kudos
Message 2 of 6
(2,133 Views)

Thanks for the help and your quick answer!

Still have some issues left. With the "Rotate 1D Array Function" the whole 1D array will be different and that's not really desired in my case. I just want to switch places between two elements in the array, with known indexes.

 

E.g. 2 2 2 2 2      --->     2 2 2 2 2

        2 1 1 1 1                 2 1 1 1 1

        2 1 1 2 2                 2 5 1 2 2

        1 1 5 2 2                 1 1 1 2 2

        1 1 1 1 1                 1 1 1 1 1

5 at index (3,2)  <---> 1 at index (2,1)

 

With 1D array. [2,2,2,2,2,2,1,1,1,1,2,1,1,2,2,1,1,5,2,2,1,1,1,1,1] 

                           5 at index 17  <---> 1 at index 11

                           --->  [2,2,2,2,2,2,1,1,1,1,2,5,1,2,2,1,1,1,2,2,1,1,1,1,1]

 

Sorry for the misleading example in my previous post...

0 Kudos
Message 3 of 6
(2,105 Views)
Solution
Accepted by topic author hannesostergren

You should really look at different Array Functions available. And instead of using Rotate 1D array function you may try out using first Delete From Array and then Insert Into Array Function.


I am not allergic to Kudos, in fact I love Kudos.

 Make your LabVIEW experience more CONVENIENT.


0 Kudos
Message 4 of 6
(2,099 Views)

Thanks for all the support! It's now solved.

0 Kudos
Message 5 of 6
(2,089 Views)

@moderator1983 wrote:

You should really look at different Array Functions available. And instead of using Rotate 1D array function you may try out using first Delete From Array and then Insert Into Array Function.


Better yet would be Index Array and Replace Array Subset!

Message 6 of 6
(2,071 Views)