From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Calculations in 2D array

Solved!
Go to solution

Hello every one,

 

I have designed a .vi code for my calculations.

but in a specific part of this code, I generate a 2d-array. Then I need to make some calculations on this array and convert it to a new array.

I am already writing this code in C++ and the results satisfy me by using root programming. but how can I perform the same calculations using the Labview? I have used the formula node but it was not helpful.

 in the following, you will find the code in C++ and the primary array is attached. 

can you help with this !!!

 

C++ code:

 

double B=1.0006; 
double E1, C1, E2, P, C, D1, D2;
double P0=0, C0=0, C2=0;
E2=int(E1);

inputfile.open("file1");
outputfile.open("file2");

while (inputfile >> E1 >> C1){
if((E2+1)>(E1+(B/2))){
C=B*C1;
C2=C2+C;}

else{
P=(E2+1)-(E1-(B/2));
C2=(C2+P*C1+P0*C0);

outputfile << E2 << " "<< C2 << endl;

C0=C1;
P0=(E1+(B/2))-(E2+1);
C2=0;
E2=E2+2;}}

inputfile.close();
outputfile.close();}

 

 

0 Kudos
Message 1 of 18
(3,534 Views)

Hi Khallod,

 

You can probably do this fairly easily using LabVIEW's Numeric palette and a loop (either For or While, depending on how you load the text file(s)? - I'd suggest reading them as "lines" and then specifying -1 as the count, this gives an array of strings with one value per line, then a For loop with autoindexing probably) and Shift Registers to handle updating values between iterations.

 

Use a Case Structure for the if/else condition and Write to Text File (or similar) to write the output file. Make sure to open before the loop and close after, and keep a reference to the file inside the loop (commonly using another Shift Register, you can also use just normal tunnels if you know that the loop has at least one iteration, but using SRs is simpler - no thought/checking required).


GCentral
0 Kudos
Message 2 of 18
(3,513 Views)

I am not fluent in C++ code. Can you instead explain it in simple terms? Maybe with a small example array and expected result.

0 Kudos
Message 3 of 18
(3,510 Views)

in the attached, you can find the full .vi code. and the original data file (original.txt).

I am tried to use the formula node as in the right part of the block diagram. but it does not work. 

for your information the first output file is perfect, but the second one is not.

and also, the attached file (output.txt file is the expected results (I generate it by C++ for the same input data))

 

please check. 

Download All
0 Kudos
Message 4 of 18
(3,503 Views)

please check the above reply

0 Kudos
Message 5 of 18
(3,501 Views)

I very nearly managed to produce your "output file.txt", but seems I'm a little off.

 

I *think* that you missed a bit of your code (that's quite relevant) - your initial "E2" value is -4, right? Your code snippet is undefined (you use E2 = int(E1), but only "double E1", so the value is I believe undefined and implementation specific. It's probably zero, but I don't *think* it's guaranteed).

 

In any case, your output file has -4 as the first E2 value, but E2 isn't modified before it would be written. So I'm not sure how you produced the output file.

 

My values are also slightly smaller, but I get the first non-zero C2 for E2=78, same as your file. My value is 2594.45, compared to your 2603.51. Using the first posted "file.txt" as the input. I'm not sure if that was the intention?


GCentral
0 Kudos
Message 6 of 18
(3,469 Views)

Your output file is similar to your input file except for the fact that the formatting is a bit difficult, a column delimiter of about 20 spaces in NOT reasonable! (I recommend logarithmic mapping for the Y axis)

 

altenbach_0-1585156596071.png

 

 

Obviously your code only generates two scalars so it is meaningless. I would strongly prefer a website or scientific paper that describes the algorithm formulas without any code or pseudocode. Does that exist?

 

Of course your entire code before the script node is pretty bad and overly complicated. You don't really have a 2D array, just a 1D array and a derived, equally spaced x-array. "delete from array" is NOT the right tool to get an array subset. Never hide the labels of terminals in the diagram.

 

I took the liberty to slightly clean up your code, but I haven't looked at the processing based on the input data. Note that your entire formula node does not really depend on the data and is is clearly impossible to generate such a rich output based on five scalars, right?

 

So once you show us the actual formulas, we can go from there. Still this gives you some ideas how to do array manipulations. I might have made small mistakes, so verify correctness.

 

 

altenbach_1-1585156753622.png

 

0 Kudos
Message 7 of 18
(3,419 Views)

Let me rephrase the inquiry in another way.

in the attached files I shared my code in C++, I make it more clear to show the idea.

I have tried many times to convert it with LabVIEW but without benefit.

should I try with the formula node or by using the case structures !! 

if you have ideas please share it with me. 

for your information, this process should be just part of my general code. I have steps before and after.

in the attached files you will find also small part of the input data and the expected results. * I am already getting these results with C++).

 

Download All
0 Kudos
Message 8 of 18
(3,377 Views)

How do you evaluate (inputfile >> E1 >> C1) and

outputfile << E2 << " "<< C2 << endl;

 

My understanding is that >> and << mean right and left shifts.  But that doesn't make sense in the context those operators were used in the program.

0 Kudos
Message 9 of 18
(3,369 Views)

Again, show us a link describing the algorithm! That's all we really need.

 

(Again, why do your files have a column delimiter of 11 spaces?)

0 Kudos
Message 10 of 18
(3,364 Views)