简体   繁体   English

使用文件将c ++数组转换为matlab大矩阵

[英]transforming c++ array to matlab big matrix using file

In my code i'm changing my array (int*) and then I want to compare it into the matlab results. 在我的代码中,我要更改数组(int *),然后将其与matlab结果进行比较。

since my array is big 1200 X 1000 element. 因为我的数组很大1200 X 1000元素 this takes forever to load it into matlab 这需要永远将其加载到matlab中

i'm trying to copy the printed output file into matlab command line... 我正在尝试将打印的输出文件复制到matlab命令行中...

for (int i = 0; i < _roiY1; i++)
{
    for (int j = 0; j < newWidth; j++)
    {
        channel_gr[i*newWidth + j] = clipLevel;
    }
}

ofstream myfile;
myfile.open("C:\\Users\\gdarmon\\Desktop\\OpenCVcliptop.txt");
for (int i = 0; i <  newHeight ; i++)
{
    for (int j = 0; j < newWidth; j++)
    {
        myfile << channel_gr[i * newWidth + j] << ", ";
    }
    myfile<<";" <<endl;
}

is there a faster way to create a readable matrix data from c++? 有没有更快的方法来从c ++创建可读的矩阵数据? into matlab? 进入Matlab?

The simplest answer is that it's much quicker to transfer the data in binary form, rather than - as suggested in the question - rendering to text and having Matlab parse it back to binary. 最简单的答案是,以二进制形式传输数据要快得多,而不是(如问题中所建议的那样)呈现为文本并让Matlab将其解析回二进制。 You can achieve this by using fwrite() at the C/C++ end, and fread() at the Matlab end. 您可以通过在C / C ++端使用fwrite()和在Matlab端使用fread()来实现此目的。

int* my_data = ...;
int my_data_count = ...;

FILE* fid = fopen('my_data_file', 'wb');
fwrite((void*)my_data, sizeof(int), my_data_count, fid);
fclose(fid);

In Matlab: 在Matlab中:

fid = fopen('my_data_file', 'r');
my_data = fread(fid, inf, '*int32');
fclose(fid);

It's maybe worth noting that you can call C/C++ functions from within Matlab, so depending on what you are doing that may be an easier architecture (look up "mex files"). 可能值得注意的是,您可以在Matlab中调用C / C ++函数,因此根据您的操作,这可能是一个更简单的体系结构(查找“ mex文件”)。

Don't write the output as text. 不要将输出写为文本。

Write your matrix into your output file the way Matlab likes to read: big array of binary. 用Matlab喜欢的方式将矩阵写入输出文件:大二进制数组。

ofstream myfile;
myfile.open("C:\\Users\\gdarmon\\Desktop\\OpenCVcliptop.txt", ofstream::app::binary);
myfile.write((char*) channel_gr, newHeight*newWidth*sizeof(channel_gr[0]));

You may want to play some games on output to get the array ordered column-row rather than row-column because of the way matlab likes to see data. 您可能要在输出上玩一些游戏,以使数组按列行而不是按行排序,这是因为matlab喜欢查看数据的方式。 I remember orders of magnitude improvements in performance when writing mex file plug-ins for file readers, but it's been a while since I've done it. 我记得为文件读取器编写mex文件插件时性能提高了几个数量级,但是自从这样做以来已经有一段时间了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM