⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 matcreat.c

📁 matlab实用教程
💻 C
字号:
#include <stdio.h>
#include <string.h>  /* 要使用到字符串操作函数strcmp()           */
#include <stdlib.h>  /* 要用到EXIT_FAILURE, EXIT_SUCCESS     */
#include "mat.h"    /* 操作MAT文件API函数的头文件,必须添加 */

#define BUFSIZE 256

int main() {
	/* 创建指向MAT文件类型的指针 */	
	MATFile *pmat;
	/* 创建必要的变量 */
	mxArray *pa1, *pa2, *pa3;
	double data[9] = { 1.0, 4.0, 7.0, 2.0, 5.0, 8.0, 3.0, 6.0, 9.0 };
	const char *file = "mattest.mat";
	char str[BUFSIZE];
	int status; 

	printf("Creating file %s...\n\n", file);
	/* 以写方式打开MAT文件,若文件不存在,则创建一个并打开 */
	pmat = matOpen(file, "w");
	/* 检查返回值,若打开失败,返回 */
	if (pmat == NULL) {
		printf("Error creating file %s\n", file);
		printf("(Do you have write permission in this directory?)\n");
		return(EXIT_FAILURE);
	}

	/* 创建3×3的矩阵 */
	pa1 = mxCreateDoubleMatrix(3,3,mxREAL);
	if (pa1 == NULL) {
		printf("%s : Out of memory on line %d\n", __FILE__, __LINE__); 
		printf("Unable to create mxArray.\n");
		return(EXIT_FAILURE);
	}

	pa2 = mxCreateDoubleMatrix(3,3,mxREAL);
	if (pa2 == NULL) {
		printf("%s : Out of memory on line %d\n", __FILE__, __LINE__);
		printf("Unable to create mxArray.\n");
		return(EXIT_FAILURE);
	}
	/* 用data的数据初始化pa3 */
	memcpy((void *)(mxGetPr(pa2)), (void *)data, sizeof(data));

	/* 创建字符串变量 */ 
	pa3 = mxCreateString("MATLAB: the language of technical computing");
	if (pa3 == NULL) {
		printf("%s :  Out of memory on line %d\n", __FILE__, __LINE__);
		printf("Unable to create string mxArray.\n");
		return(EXIT_FAILURE);
	}

	/* 将pa1指向的数据写入MAT文件中 */
	status = matPutVariable(pmat, "LocalDouble", pa1);
	if (status != 0) {
		printf("%s :  Error using matPutVariable on line %d\n", __FILE__, __LINE__);
		return(EXIT_FAILURE);
	}  

	/* 将pa2指向的数据写入MAT文件中 */
	status = matPutVariableAsGlobal(pmat, "GlobalDouble", pa2);
	if (status != 0) {
		printf("Error using matPutVariableAsGlobal\n");
		return(EXIT_FAILURE);
	} 

	/* 将pa3指向的数据写入MAT文件中 */
	status = matPutVariable(pmat, "LocalString", pa3);
	if (status != 0) {
		printf("%s :  Error using matPutVariable on line %d\n", __FILE__, __LINE__);
		return(EXIT_FAILURE);
	} 

	/* 下面代码演示了matPutVariable会覆盖MAT文件中已有的数据  */
	memcpy((void *)(mxGetPr(pa1)), (void *)data, sizeof(data));
	status = matPutVariable(pmat, "LocalDouble", pa1);
	if (status != 0) {
		printf("%s :  Error using matPutVariable on line %d\n", __FILE__, __LINE__);
		return(EXIT_FAILURE);
	} 

	/* 清理数据 */
	mxDestroyArray(pa1);
	mxDestroyArray(pa2);
	mxDestroyArray(pa3);

	/* 关闭MAT文件 */
	if (matClose(pmat) != 0) {
		printf("Error closing file %s\n",file);
		return(EXIT_FAILURE);
	}

	/* 以只读方式重新打开MAT文件,用matGetVariable函数验证其内容 */
	pmat = matOpen(file, "r");
	if (pmat == NULL) {
		printf("Error reopening file %s\n", file);
		return(EXIT_FAILURE);
	}

	/* 读取刚刚写入的变量 pa1、pa2、pa3,检查是否已成功写入 */
	pa1 = matGetVariable(pmat, "LocalDouble");
	if (pa1 == NULL) {
		printf("Error reading existing matrix LocalDouble\n");
		return(EXIT_FAILURE);
	}
	if (mxGetNumberOfDimensions(pa1) != 2) {
		printf("Error saving matrix: result does not have two dimensions\n");
		return(EXIT_FAILURE);
	}

	pa2 = matGetVariable(pmat, "GlobalDouble");
	if (pa2 == NULL) {
		printf("Error reading existing matrix GlobalDouble\n");
		return(EXIT_FAILURE);
	}
	if (!(mxIsFromGlobalWS(pa2))) {
		printf("Error saving global matrix: result is not global\n");
		return(EXIT_FAILURE);
	}

	pa3 = matGetVariable(pmat, "LocalString");
	if (pa3 == NULL) {
		printf("Error reading existing matrix LocalString\n");
		return(EXIT_FAILURE);
	}

	status = mxGetString(pa3, str, sizeof(str));
	if(status != 0) {
		printf("Not enough space. String is truncated.");
		return(EXIT_FAILURE);
	}
	if (strcmp(str, "MATLAB: the language of technical computing")) {
		printf("Error saving string: result has incorrect contents\n");
		return(EXIT_FAILURE);
	}

	/* 退出前清理临时变量 */
	mxDestroyArray(pa1);
	mxDestroyArray(pa2);
	mxDestroyArray(pa3);

	/* 关闭MAT文件 */
	if (matClose(pmat) != 0) {
		printf("Error closing file %s\n",file);
		return(EXIT_FAILURE);
	}
	printf("Done\n");
	return(EXIT_SUCCESS);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -