c++ 与 Matlab 程序的数据比对

文章目录

    • 背景
    • 环境
    • 数据保存
    • 数据加载

背景

***避免数据精度误差,快速对比变量 ***

环境

c++下载 https://github.com/BlueBrain/HighFive
以及hdf5库

在vs 中配置库
c++ 与 Matlab 程序的数据比对_第1张图片

数据保存

#include 
using namespace HighFive;

std::string filename1 = "test.h5";
File file1(filename1, File::Truncate);
//保存数据
file1.createDataSet("TempMS2Mz_SubRun" , TempMS2Mz_SubRun);
% 定义 HDF5 文件路径
h5_file_path = 'example.h5';

% 写入第一个数据集
data1 = rand(3, 3);
h5create(h5_file_path, '/dataset1', size(data1));
h5write(h5_file_path, '/dataset1', data1);

% 写入第二个数据集
data2 = magic(4);
h5create(h5_file_path, '/dataset2', size(data2));
h5write(h5_file_path, '/dataset2', data2);

disp('多个数据集已写入 HDF5 文件。');

数据加载

#include 
using namespace HighFive;

std::string filename1 = "test.h5";
File file1(filename1, HighFive::File::ReadOnly);
auto dataset = file.getDataSet("grp/data");

// Read back, automatically allocating:
auto data = dataset.read<std::vector<int>>();

// Alternatively, if `data` has the correct
// size, without reallocation:
dataset.read(data);
% 定义 HDF5 文件路径
h5_file_path = 'example.h5';

% 读取第一个数据集
data1 = h5read(h5_file_path, '/dataset1');
disp('dataset1:');
disp(data1);

% 读取第二个数据集
data2 = h5read(h5_file_path, '/dataset2');
disp('dataset2:');
disp(data2);

你可能感兴趣的:(c++,matlab,开发语言)