Creating Matlab Structure in C++

Hi,

I have used MATLAB to simulate Wireless Sensor Networks (WSNs) for a long time, but since it is too slow, I plan to simulate them using C++. In this regards, I have following questions:

1- In MATLAB, I employed a struct (called S) in order to save the information of different sensors. For example, sensor 1 has some information, such as the residual energy (S(1).Eres), distance to base station (S(1).distance), etc. Now, in C++, I have written a struct as follows:

struct sensor {
char type;
int nhop, nhop_dist, Rc, CH, CH_dist, Nd, CM, Lv, T, relay, loc;
float Eres, xd, yd, distance;
int nbr[30];
void tracking();
};

In my main, I have defined an object called S and used rand function to create a random WSN:

int main()
{
struct sensor S[N]; // N is the number of nodes
int i;
for (i=1; i <= N; i++)
{
S[i].xd= rand() % M + 1; // location of nodes
S[i].yd= rand() % M + 1;
S[i].Eres=Emax;
}
return 0;
}

Now, my question is that how can I define multiple sensor nodes as what I used to define in MATLAB? Is what I have used correct?

2- How can I pass S to a function? Since I want to access all sensor nodes, I should have access to all nodes in the function. Could you please write an example?

Thanks in advance
First things first, in C++, an array with N items is accessed using indexes 0 through N-1, not 1 through N.

Second, unless memory is a concern, use type double instead of float. It's higher precision and there's really no down side.

You can pass the the array to a function with something like this:
1
2
3
4
void func(sensor S[], unsigned size)
{
    do stuff with S;
}


But you will probably find it easier to use a vector<S> instead of an array. The advantage of a vector is that it can be as big (or small) as you want.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <vector>
#include <cmath>

using std::vector;

struct sensor {
    char type;
    int nhop, nhop_dist, Rc, CH, CH_dist, Nd, CM, Lv, T, relay, loc;
    double Eres, xd, yd, distance;
    int nbr[30];
    void tracking();
};

// Compute and distance from xd and yd
void computeDistance(vector<sensor> &S)
{
    for (int i=0; i < S.size(); ++i) {
        S[i].distance = sqrt(S[i].xd*S[i].xd + S[i].yd*S[i].yd);
    }
}


int main()
{
    const unsigned N=10;
    const unsigned M=10000;
    const double Emax = 12345.6789;
    vector <sensor> S;

    for (int i=0; i < N; i++) {
        struct sensor dummy;
        dummy.xd= rand() % M + 1; // location of nodes
        dummy.yd= rand() % M + 1;
        dummy.Eres=Emax;
        S.push_back(dummy);
    }
    computeDistance(S);
}

Thanks for the answer. It worked well.

Now, I am collecting the results in order to depict the figures. To do so, I use MATLAB, which is very professional in plotting the results. In order to collect the results and pass them to MATLAB, I first define some global output variables, as text files, at the beginning of the code as:
1
2
3
4
ofstream out1;
ofstream out2;
ofstream out3;
ofstream out4;


Then, since I want to run a program for 10 times, for example, and then average the results, I use an if-then structure like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
     if (run == 0){
    out1.open("D:\\C++ Programming\\LEACH\\LEACH\\Results\\R100\\alive1001.txt", ios::out);
    out2.open("D:\\C++ Programming\\LEACH\\LEACH\\Results\\R100\\cep1001.txt", ios::out);
    out3.open("D:\\C++ Programming\\LEACH\\LEACH\\Results\\R100\\chs1001.txt", ios::out);
    out4.open("D:\\C++ Programming\\LEACH\\LEACH\\Results\\R100\\thr1001.txt", ios::out);
    }
    else if (run == 1){
    out1.open("D:\\C++ Programming\\LEACH\\LEACH\\Results\\R100\\alive1002.txt", ios::out);
    out2.open("D:\\C++ Programming\\LEACH\\LEACH\\Results\\R100\\cep1002.txt", ios::out);
    out3.open("D:\\C++ Programming\\LEACH\\LEACH\\Results\\R100\\chs1002.txt", ios::out);
    out4.open("D:\\C++ Programming\\LEACH\\LEACH\\Results\\R100\\thr1002.txt", ios::out);
    }
...

    for (int i=0;i<ALIVE.size();i++){
    out1<<ALIVE[i]<< endl;
    out2<<CEP[i]<< endl;
    out3<<CHS[i]<< endl;
    out4<<THR[i]<< endl;
    }

    out1.close();
    out2.close();
    out3.close();
    out4.close();


The problem with this is that the program creates an alive1001.txt, then adds the result of the second run to alive1002.txt. That is, alive1002.txt contains the information of both alive1001 and alive1002 txt files, and so on. Could you please tell me what is the problem?

Moreover, is there a more convenient way to collect all results, e.g., using a loop to create files, and pass them to MATLAB? I just found txt files as a simple method to do so.

Many thanks
alive1002.txt contains the information of both alive1001 and alive1002 txt files

You say you're running the program 10 times. Does that mean you are literally executing the program 10 separate times, or is there a big for (run =0; run < 10; ++run) loop somewhere?

If you have a for loop for run then the problem is that you aren't clearing the ALIVE array (and probably other arrays too) between runs. So each run appends data to ALIVE instead of creating an entirely new set.
Yes, you are right! Thank you.

Just one question. Is there a simpler way to define the name of each output (like using a loop) file rather than defining it by if-then? If I want to run the program for 50 times, I have to use a lot of if-then so.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
#include <fstream>

constexpr auto TRIALS = 50;

int main()
{
    for (int RUN = 0; RUN < TRIALS; ++RUN)
    {
        //declare RUN dependant file name
        std::string fileName = "D:\\input1" + std::to_string(RUN) + ".txt";
        //initialize out1 with fileName
        std::ofstream out1(fileName);
        //do stuff with out1 ... etc
    }
}
Hey Jack... I have a library that lets you convert M files to cpp files with some hands-on changes but not as many as you might think.... I converted about 100 over a couple of years back in the day...
Topic archived. No new replies allowed.