cattle421 发表于 2006-12-20 20:12

请教如何将wave文件变成matlab数据文件?

请教如何将wave文件变成matlab数据文件?也就是变成Matlab data file,而不是matlab M-file?非常感谢!

xl_43400 发表于 2006-12-20 22:39

可以先在VC++中把wave文件数据读到一个数组中,然后用c语言把这个数组写成.mat文件格式.
在VC++中读写mat文件的代码可以参考如下代码:
//////////////////////////////////////
/*
* MAT-file creation program
*
* See the MATLAB API Guide for compiling information.
*
* Calling syntax:
*
*   matcreat
*
* Create a MAT-file which can be loaded into MATLAB.
*
* This program demonstrates the use of the following functions:
*
*matClose
*matGetVariable
*matOpen
*matPutVariable
*matPutVariableAsGlobal
*
* Copyright 1984-2000 The MathWorks, Inc.
* $ Revision: 1.13 $
*/
#include <stdio.h>
#include <string.h> /* For strcmp() */
#include <stdlib.h> /* For EXIT_FAILURE, EXIT_SUCCESS */
#include "mat.h"

#define BUFSIZE 256

int main() {
MATFile *pmat;
mxArray *pa1, *pa2, *pa3;
double data = { 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;
int status;

printf("Creating file %s...\n\n", file);
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);
}

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);
}
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);
}

status = matPutVariable(pmat, "LocalDouble", pa1);
if (status != 0) {
      printf("%s :Error using matPutVariable on line %d\n",
            __FILE__, __LINE__);
      return(EXIT_FAILURE);
}

status = matPutVariableAsGlobal(pmat, "GlobalDouble", pa2);
if (status != 0) {
      printf("Error using matPutVariableAsGlobal\n");
      return(EXIT_FAILURE);
}

status = matPutVariable(pmat, "LocalString", pa3);
if (status != 0) {
      printf("%s :Error using matPutVariable on line %d\n",
            __FILE__, __LINE__);
      return(EXIT_FAILURE);
}

/*
   * Ooops! we need to copy data before writing the array.(Well,
   * ok, this was really intentional.) This demonstrates that
   * matPutVariable will overwrite an existing array in a
MAT-file.
   */
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);
}

/* Clean up. */
mxDestroyArray(pa1);
mxDestroyArray(pa2);
mxDestroyArray(pa3);

if (matClose(pmat) != 0) {
    printf("Error closing file %s\n",file);
    return(EXIT_FAILURE);
}

/* Re-open file and verify its contents with matGetVariable. */
pmat = matOpen(file, "r");
if (pmat == NULL) {
    printf("Error reopening file %s\n", file);
    return(EXIT_FAILURE);
}

/* Read in each array we just wrote. */
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);
}

/* Clean up before exit. */
mxDestroyArray(pa1);
mxDestroyArray(pa2);
mxDestroyArray(pa3);

if (matClose(pmat) != 0) {
    printf("Error closing file %s\n",file);
    return(EXIT_FAILURE);
}
printf("Done\n");
return(EXIT_SUCCESS);
}
///////////////////////////////////////////////////
希望对你有帮助哦

eight 发表于 2006-12-21 11:41

原帖由 cattle421 于 2006-12-20 20:12 发表
请教如何将wave文件变成matlab数据文件?也就是变成Matlab data file,而不是matlab M-file?非常感谢!


没有记错的话,matlab中有相应的函数读取,搜索一下版面吧

cattle421 发表于 2006-12-21 14:39

多谢各位,VC我不是太懂,楼上的方法我试过好像只能存为matlab M-file,没有Matlab data file

mulan 发表于 2006-12-21 14:58

用wavread函数读进来,然后再save成dat文件应该没有问题啊

cattle421 发表于 2006-12-22 13:13

“然后再save成dat文件应该没有问题啊”
请问楼上,这句话用语句怎么写?

mulan 发表于 2006-12-22 15:17

原帖由 cattle421 于 2006-12-22 13:13 发表
“然后再save成dat文件应该没有问题啊”
请问楼上,这句话用语句怎么写?

比如你的数据在矩阵a中
save('a.mat',a)
数据就保存在当前工作目录下的a.mat文件里了

cattle421 发表于 2006-12-23 16:24

原帖由 cattle421 于 2006-12-22 13:13 发表
“然后再save成dat文件应该没有问题啊”
请问楼上,这句话用语句怎么写?

这个就是我题目上说的啊,存为的是matlab M-file,好像不是Matlab data file
不知道是我概念不清还是这两个就是一回事?

xl_43400 发表于 2006-12-23 22:07

.mat 好象是数据文件哦
只能按照特定的数据结构写吧?

eight 发表于 2006-12-24 21:57

原帖由 mulan 于 2006-12-22 15:17 发表


比如你的数据在矩阵a中
save('a.mat',a)
数据就保存在当前工作目录下的a.mat文件里了


应该是save('a.mat','a')

健康第一 发表于 2007-2-28 13:22

请问如何在VC++中将wave文件格式转换成数组呢,然后怎么写成.mat格式呢??
页: [1]
查看完整版本: 请教如何将wave文件变成matlab数据文件?