Demonstrates how to run the C++ code and visualize the results using Matlab.
Contents
This example demonstrates how to:
- Read data from a file,
- Use Persistence1D class to extract extrema
- Filter results according to persistence values.
- Get results in Matlab output format
- Visualize results using Matlab scripts.
Expected Results
Reference output file: [persistence_base_dir]\src\examples\MatlabVisualization\MatlabVisualizationRes.ref with contents:
25
27
5
4
9
8
17
19
13
11
2
14
29
23
22
Results Visualization
- Start Matlab
- Change the directory to [persistence_base_dir]\matlab
- Run: visualize_features('..\data.txt', '..\res.txt')
Matlab Plot of Results
Matlab Plot of Results
Code Documentation
#include "..\..\persistence1d\persistence1d.hpp"
#include <fstream>
using namespace std;
using namespace p1d;
{
vector<float> data;
float currdata;
ifstream datafile;
ofstream outfile;
char * filename = "data.txt";
char * outfilename = "res.txt";
bool enableMatlabIndexing = true;
vector<int> min, max;
int globalMinIndex;
datafile.open(filename);
if (!datafile)
{
cout << "Cannot open data file for reading: " << filename << endl;
return -1;
}
while(datafile >> currdata)
{
data.push_back(currdata);
}
datafile.close();
{
cout << "ERROR" << endl;
}
float filterThreshold = 1.0;
outfile.open(outfilename);
if (!outfile)
return -2;
for (unsigned int i = 0; i < min.size() && i < max.size(); i++)
{
outfile << min[i] << endl;
outfile << max[i] << endl;
}
outfile << globalMinIndex << endl;
outfile.close();
return 0;
}