- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我基本上是在尝试将 Matlab 代码翻译成 C 代码。这是我之前的扩展 question .
在 Matlab 中,我使用了cell-structures
,它包含可变大小的矩阵( double )
。这是我的 *.mat 文件应该存储
的玩具示例:
Matlab代码:
A = [[1 2 3]; [5 7 1]; [3 5 9]];
B = [[2 4];[5 7]];
Creator = 'DKumar';
nFilters = 2;
Filters{1} = [[-1.0 -1.0 -1.0]; [-1.0 8 -1.0]; [-1.0 -1.0 -1.0]];
Filters{2} = 2.0*[[-1.0 -1.0]; [-1.0 8]; [-1.0 -1.0]];
cd('/home/dkumar/CPP_ExampleCodes_DKU/Read_mat_File');
save('Test_FILE.mat', 'A', 'B', 'Creator', 'nFilters', 'Filters');
C 代码:“matread_Matrix”函数正确读取存储在 *.mat 中的矩阵
。应该读取单元格结构的函数 "matread_Cell"
不工作
。
#include <stdio.h>
#include <stdlib.h>
#include "/usr/local/MATLAB/R2011b/extern/include/mat.h"
mxArray *arr;
mxArray *C_CELL;
/* declare a 2 x 1 array of pointers to access the cell array in C */
mxArray *cellArray[2];
struct stDoubleMat{
double* pValueInField;
int nRows, nCols;
};
void matread_Matrix(const char *file, const char *FieldName2Read, struct stDoubleMat* poDoubleMat_LOC)
{
printf("Reading file %s...\n\n", file);
//Open file to get directory
MATFile* pmat = matOpen(file, "r");
if (pmat == NULL) {
printf("Error opening file %s\n", file);
return;
}
// extract the specified variable
arr = matGetVariable(pmat, FieldName2Read);
double *pr;
if (arr != NULL && !mxIsEmpty(arr)) {
// copy data
mwSize num = mxGetNumberOfElements(arr);
pr = mxGetPr(arr);
if (pr != NULL) {
poDoubleMat_LOC->pValueInField = pr;
poDoubleMat_LOC->nRows = mxGetM(arr);
poDoubleMat_LOC->nCols = mxGetN(arr);
}
printf("matread_Matrix \n") ;
printf( "oDoubleMat_LOC.nRows %i ; oDoubleMat_LOC.nCols %i \n", poDoubleMat_LOC->nRows , poDoubleMat_LOC->nCols);
}else{
printf("nothing to read \n") ;
}
// close the file
matClose(pmat);
return;
}
void matread_Cell(const char *file, const char *FieldName2Read, int CellIndex)
{
printf("Reading file %s...\n\n", file);
//Open file to get directory
MATFile* pmat = matOpen(file, "r");
if (pmat == NULL) {
printf("Error opening file %s\n", file);
return;
}
// extract the specified variable
C_CELL = matGetVariable(pmat, FieldName2Read);
cellArray[CellIndex] = mxGetCell(C_CELL, CellIndex);
double* p2 = (double*)cellArray[CellIndex];
int nRows = mxGetM(cellArray[CellIndex]);
int nCols = mxGetN(cellArray[CellIndex]);
printf(" From inside matread_Cell : nRows %i and nCols %i \n", nRows, nCols);
int i2;
for (i2 = 0; i2 < nRows*nCols; i2++)
{
printf(" copied value : %f \n", *p2);
p2 = p2 +1;
}
// close the file
matClose(pmat);
}
int main(int argc, char **argv)
{
const char *FileName = "/home/dkumar/CPP_ExampleCodes_DKU/Read_mat_File/Test_FILE.mat";
const char *FieldName2Read = "A";
struct stDoubleMat oDoubleMat;
matread_Matrix(FileName, FieldName2Read, &oDoubleMat);
double* v = oDoubleMat.pValueInField;
printf("From main \n");
printf( "oDoubleMat.nRows %i ; oDoubleMat.nCols %i \n", oDoubleMat.nRows , oDoubleMat.nCols);
int i;
for (i = 0; i < oDoubleMat.nCols*oDoubleMat.nRows; i++)
{
printf(" copied value : %f \n", *v);
v = v +1;
}
// Reading the structure
const char *FieldName2Read2 = "Filters";
matread_Cell(FileName, FieldName2Read2, 0);
matread_Cell(FileName, FieldName2Read2, 1);
// cleanup the mex-array
mxDestroyArray(arr);
mxDestroyArray(C_CELL);
/* How to delete mxArray of pointer : should this be a array of pointers */
//mxDestroyArray(cellArray[0]);
//mxDestroyArray(cellArray[1]);
return 0;
}
输出:
$ gcc -g -o Test Read_MatFile_DKU_2.c -I/usr/local/MATLAB/R2011b/extern/include -L/usr/local/MATLAB/R2011b/bin/glnxa64 -lmat -lmx
$ ./Test
Reading file /home/dkumar/CPP_ExampleCodes_DKU/Read_mat_File/Test_FILE.mat...
matread_Matrix
oDoubleMat_LOC.nRows 3 ; oDoubleMat_LOC.nCols 3
From main
oDoubleMat.nRows 3 ; oDoubleMat.nCols 3
copied value : 1.000000
copied value : 5.000000
copied value : 3.000000
copied value : 2.000000
copied value : 7.000000
copied value : 5.000000
copied value : 3.000000
copied value : 1.000000
copied value : 9.000000
Reading file /home/dkumar/CPP_ExampleCodes_DKU/Read_mat_File/Test_FILE.mat...
From inside matread_Cell : nRows 3 and nCols 3
copied value : 0.000000
copied value : 0.000000
copied value : 0.000000
copied value : 0.000000
copied value : 0.000000
copied value : 0.000000
copied value : 0.000000
copied value : 0.000000
copied value : 0.000000
Reading file /home/dkumar/CPP_ExampleCodes_DKU/Read_mat_File/Test_FILE.mat...
From inside matread_Cell : nRows 3 and nCols 2
copied value : 0.000000
copied value : 0.000000
copied value : 0.000000
copied value : 0.000000
copied value : 0.000000
copied value : 0.000000
此外,我也无法正确读取此字段:Creator = 'DKumar';
更新:
根据@Sherwin 的建议
我的 C 代码:
#include <stdio.h>
#include <stdlib.h>
#include "/usr/local/MATLAB/R2011b/extern/include/mat.h"
mxArray *arr;
mxArray *C_CELL;
/* declare a 2 x 1 array of pointers to access the cell array in C */
mxArray *cellArray[2];
struct stDoubleMat{
double* pValueInField;
int nRows, nCols;
};
void matread_Matrix(MATFile* pmat , const char *FieldName2Read, struct stDoubleMat* poDoubleMat_LOC)
{
// extract the specified variable
arr = matGetVariable(pmat, FieldName2Read);
double *pr;
if (arr != NULL && !mxIsEmpty(arr)) {
// copy data
mwSize num = mxGetNumberOfElements(arr);
pr = mxGetPr(arr);
if (pr != NULL) {
poDoubleMat_LOC->pValueInField = pr;
poDoubleMat_LOC->nRows = mxGetM(arr);
poDoubleMat_LOC->nCols = mxGetN(arr);
}
printf("matread_Matrix \n") ;
printf( "oDoubleMat_LOC.nRows %i ; oDoubleMat_LOC.nCols %i \n", poDoubleMat_LOC->nRows , poDoubleMat_LOC->nCols);
}else{
printf("nothing to read \n") ;
}
return;
}
void matread_String(MATFile* pmat , const char *FieldName2Read)
{
// extract the specified variable
arr = matGetVariable(pmat, FieldName2Read);
double *pr;
if (arr != NULL && !mxIsEmpty(arr)) {
// copy data
mwSize num = mxGetNumberOfElements(arr);
pr = mxGetPr(arr);
if (pr != NULL) {
char *p2 = (char*) pr;
// Printing and checking
int i2;
for (i2 = 0; i2 < num; i2++)
{
printf(" copied value : %s \n", p2);
p2 = p2 +1;
}
}
}else{
printf("nothing to read \n") ;
}
return;
}
void matread_Cell(MATFile* pmat , const char *FieldName2Read, int CellIndex)
{
// extract the specified variable
C_CELL = matGetVariable(pmat, FieldName2Read);
cellArray[CellIndex] = mxGetCell(C_CELL, CellIndex);
double *p2 = (double*) mxGetPr(cellArray[CellIndex]);
int nRows = mxGetM(cellArray[CellIndex]);
int nCols = mxGetN(cellArray[CellIndex]);
printf(" From inside matread_Cell : nRows %i and nCols %i \n", nRows, nCols);
int i2;
for (i2 = 0; i2 < nRows*nCols; i2++)
{
printf(" copied value : %f \n", *p2);
p2 = p2 +1;
}
}
int main(int argc, char **argv)
{
const char *FileName = "/home/dkumar/CPP_ExampleCodes_DKU/Read_mat_File/Test_FILE.mat";
const char *FieldName2Read = "A";
//Open file to get directory
printf("Reading file %s...\n\n", FileName);
MATFile* pmat = matOpen(FileName, "r");
if (pmat == NULL) {
printf("Error opening file %s\n", FileName);
return;
}
struct stDoubleMat oDoubleMat;
matread_Matrix(pmat, FieldName2Read, &oDoubleMat);
double* v = oDoubleMat.pValueInField;
int i;
for (i = 0; i < oDoubleMat.nCols*oDoubleMat.nRows; i++)
{
printf(" copied value : %f \n", *v);
v = v +1;
}
// Reading the structure
const char *FieldName2Read2 = "Filters";
matread_Cell(pmat, FieldName2Read2, 0);
matread_Cell(pmat, FieldName2Read2, 1);
// Reading the string
const char *FieldName2Read3 = "Creator";
matread_String(pmat, FieldName2Read3);
// cleanup the mex-array
mxDestroyArray(arr);
mxDestroyArray(C_CELL);
/* How to delete mxArray of pointer : should this be a array of pointers */
//mxDestroyArray(cellArray[0]);
//mxDestroyArray(cellArray[1]);
// close the file
matClose(pmat);
return 0;
}
输出:
oDoubleMat.nRows 3 ; oDoubleMat.nCols 3
copied value : 1.000000
copied value : 5.000000
copied value : 3.000000
copied value : 2.000000
copied value : 7.000000
copied value : 5.000000
copied value : 3.000000
copied value : 1.000000
copied value : 9.000000
From inside matread_Cell : nRows 3 and nCols 3
copied value : -1.000000
copied value : -1.000000
copied value : -1.000000
copied value : -1.000000
copied value : 8.000000
copied value : -1.000000
copied value : -1.000000
copied value : -1.000000
copied value : -1.000000
From inside matread_Cell : nRows 3 and nCols 2
copied value : -2.000000
copied value : -2.000000
copied value : -2.000000
copied value : -2.000000
copied value : 16.000000
copied value : -2.000000
copied value : D
copied value :
copied value : K
copied value :
copied value : u
copied value :
copied value :
问题:1) creator 中存储的字符串值没有正确显示。
2) 如何删除cellArray[2])?
最佳答案
事实证明,稍作改动,您的代码就可以工作了:
在函数“void matread_Cell
”中,将行 double* p2 = (double*)cellArray[CellIndex];
替换为:
p2 = (double*) mxGetPr(cellArray[CellIndex]);
我查过了。它完成了工作。
同样要读取创建者字段,类似于 mtread_matrix
的代码应该可以工作,只是类型是 char*
而不是 double*
(不过我没有检查这个。如果它不起作用请告诉我)。
更新:您可以使用以下代码读取字符串。 (引用:here)
void matread_string(const char *file, const char *FieldName2Read, char *pr, mwSize *len)
{
printf("Reading file %s...\n\n", file);
//Open file to get directory
MATFile* pmat = matOpen(file, "r");
if (pmat == NULL) {
printf("Error opening file %s\n", file);
return;
}
// extract the specified variable
arr = matGetVariable(pmat, FieldName2Read);
if (arr != NULL && !mxIsEmpty(arr)) {
// copy data
mwSize num = mxGetNumberOfElements(arr);
//int mxGetString(const mxArray *pm, char *str, mwSize strlen);
int res= mxGetString(arr, pr, num+1); //strlen should be len+1. c.f. reference.
if(res==0)
printf("success!\n");
else
printf("failed.\n");
if ( pr == NULL){
printf("null pointer.\n");
}
printf("matread_string \n") ;
printf( "len: %i \n", (int)num);
*len=num;
}else{
printf("nothing to read \n") ;
}
// close the file
matClose(pmat);
return;
}
在 main
中,您可以像这样使用它:
const char *FieldName2Read3 = "Creator";
char pr[20];
mwSize len;
matread_string(FileName, FieldName2Read3, pr, &len);
//int i;
printf(" copied value: %s \n",pr);
for (i = 0; (mwSize) i < len; i++)
{
printf(" copied value : %c \n", pr[i]);
}
关于取消分配 cellArray,我收到错误消息:“未分配正在释放的指针”,因此我认为您不需要释放它。另一个释放动态内存的有用命令是:void mxFree(void *ptr);
关于 mexPrintf
函数,我实际上可以使用它。我刚刚收到警告 implicit declaration of function 'mexPrintf' is invalid in C99 [-Wimplicit-function-declaration]
,因为我是通过 gcc 而不是 mex 编译的。如果您使用的是 gcc,您可能需要包含正确的库来识别该函数。你可能会发现 this很有用,因为它对我有用。
关于c - 使用 C : how to read cell-structure properly 读取 .mat 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29595357/
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!