?? creating a mat-file in c.htm
字號:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0084)http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/ch05mat8.shtml -->
<HTML><HEAD><TITLE>Importing and Exporting Data (External Interfaces/API)</TITLE>
<META content="text/html; charset=gb2312" http-equiv=Content-Type><LINK
href="Creating a MAT-File in C.files/docset.css" rel=stylesheet type=text/css><!-- $Revision$ $Date$ --><!-- DOCNAME: External Interfaces/API --><!-- CHUNKNAME: Examples of MAT-Files --><!-- CHAPNAME: Importing and Exporting Data --><!-- HEADSTUFF -->
<META content="MSHTML 5.00.3315.2870" name=GENERATOR></HEAD>
<BODY bgColor=#ffffff class=support><!-- NAVBARTOP -->
<SCRIPT language=Javascript>if (parent.frames.length<2) document.write('<p><a href="/access/helpdesk/help/helpdesk.shtml" target=_top><b>Documentation</b></a> <img src="/access/helpdesk/help/arrowr.gif"> <a href="/access/helpdesk/help/techdoc/matlab.shtml" target=_top><b>MATLAB</b></a> <img src="/access/helpdesk/help/arrowr.gif"> <a href="matlab_external.shtml"><b>External Interfaces/API</b></a></p>');</SCRIPT>
<TABLE border=0 cellPadding=0 cellSpacing=0 class=support width="100%">
<TBODY>
<TR>
<TD bgColor=#d9e1e1 vAlign=baseline><B>External Interfaces/API</B></TD>
<TD align=right bgColor=#d9e1e1 vAlign=baseline><A
href="http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/ch05mat7.shtml"><IMG
border=0
src="Creating a MAT-File in C.files/b_prev.gif"></A> <A
href="http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/ch05mat9.shtml"><IMG
border=0
src="Creating a MAT-File in C.files/b_next.gif"></A></TD></TR></TBODY></TABLE><A
name=examples_of_mat-files></A><!-- H1 --><A name=14500></A>
<P><FONT class=Head2 size=+2><B>Examples of MAT-Files</B></FONT><BR>
<P><A name=20317></A>This section includes C and Fortran examples of writing,
reading, and diagnosing MAT-files. The examples cover the following topics:</P>
<UL>
<LI><A name=32478></A><A
href="http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/ch05mat8.shtml#25823">Creating
a MAT-File in C</A>
<LI><A name=32483></A><A
href="http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/ch05mat9.shtml#32276">Reading
a MAT-File in C</A>
<LI><A name=32488></A><A
href="http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/ch05ma10.shtml#14805">Creating
a MAT-File in Fortran</A>
<LI><A name=32493></A><A
href="http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/ch05ma11.shtml#27157">Reading
a MAT-File in Fortran</A> </LI></UL><A name=creating_a_mat-file_in_c></A><!-- H2 --><A name=25823></A>
<P><FONT class=Head3 size=+1><B>Creating a MAT-File in C</B></FONT><BR>
<P><A name=32850></A>This sample program illustrates how to use the library
routines to create a MAT-file that can be loaded into MATLAB. The program also
demonstrates how to check the return values of MAT-function calls for read or
write failures.</P>
<UL class=continued>
<LI class=continued><PRE><A name=32856></A>/*
<A name=32857></A> * MAT-file creation program
<A name=32858></A> *
<A name=32859></A> * See the MATLAB API Guide for compiling information.
<A name=32860></A> *
<A name=32861></A> * Calling syntax:
<A name=32862></A> *
<A name=32863></A> * matcreat
<A name=32864></A> *
<A name=32865></A> * Create a MAT-file which can be loaded into MATLAB.
<A name=32866></A> *
<A name=32867></A> * This program demonstrates the use of the following functions:
<A name=32868></A> *
<A name=32869></A> * matClose
<A name=32870></A> * matGetVariable
<A name=32871></A> * matOpen
<A name=32872></A> * matPutVariable
<A name=32873></A> * matPutVariableAsGlobal
<A name=32874></A> *
<A name=32875></A> * Copyright 1984-2000 The MathWorks, Inc.
<A name=39989></A> * $Revision: 1.9 $
<A name=32876></A> */
<A name=32878></A>#include <stdio.h>
<A name=32879></A>#include <string.h> /* For strcmp() */
<A name=32880></A>#include <stdlib.h> /* For EXIT_FAILURE, EXIT_SUCCESS */
<A name=32881></A>#include "mat.h"
<A name=32882></A>
<A name=32883></A>#define BUFSIZE 256
<A name=32884></A>
<A name=40002></A>int main() {
<A name=40003></A> MATFile *pmat;
<A name=40004></A> mxArray *pa1, *pa2, *pa3;
<A name=40005></A> double data[9] = { 1.0, 4.0, 7.0, 2.0, 5.0, 8.0, 3.0, 6.0, 9.0 };
<A name=40006></A> const char *file = "mattest.mat";
<A name=40007></A> char str[BUFSIZE];
<A name=40008></A> int status;
<A name=40009></A>
<A name=40010></A> printf("Creating file %s...\n\n", file);
<A name=40011></A> pmat = matOpen(file, "w");
<A name=40012></A> if (pmat == NULL) {
<A name=40013></A> printf("Error creating file %s\n", file);
<A name=40014></A> printf("(Do you have write permission in this directory?)\n");
<A name=40015></A> return(EXIT_FAILURE);
<A name=40016></A> }
<A name=40017></A>
<A name=40018></A> pa1 = mxCreateDoubleMatrix(3,3,mxREAL);
<A name=40019></A> if (pa1 == NULL) {
<A name=40020></A> printf("%s : Out of memory on line %d\n", __FILE__,
__LINE__);
<A name=40021></A> printf("Unable to create mxArray.\n");
<A name=40022></A> return(EXIT_FAILURE);
<A name=40023></A> }
<A name=40024></A>
<A name=40025></A> pa2 = mxCreateDoubleMatrix(3,3,mxREAL);
<A name=40026></A> if (pa2 == NULL) {
<A name=40027></A> printf("%s : Out of memory on line %d\n", __FILE__,
__LINE__);
<A name=40028></A> printf("Unable to create mxArray.\n");
<A name=40029></A> return(EXIT_FAILURE);
<A name=40030></A> }
<A name=40031></A> memcpy((void *)(mxGetPr(pa2)), (void *)data, sizeof(data));
<A name=40032></A>
<A name=40033></A> pa3 = mxCreateString("MATLAB: the language of technical
computing");
<A name=40034></A> if (pa3 == NULL) {
<A name=40035></A> printf("%s : Out of memory on line %d\n", __FILE__,
__LINE__);
<A name=40036></A> printf("Unable to create string mxArray.\n");
<A name=40037></A> return(EXIT_FAILURE);
<A name=40038></A> }
<A name=40039></A>
<A name=40040></A> status = matPutVariable(pmat, "LocalDouble", pa1);
<A name=40041></A> if (status != 0) {
<A name=40042></A> printf("%s : Error using matPutVariable on line %d\n",
__FILE__, __LINE__);
<A name=40043></A> return(EXIT_FAILURE);
<A name=40044></A> }
<A name=40045></A>
<A name=40046></A> status = matPutVariableAsGlobal(pmat, "GlobalDouble", pa2);
<A name=40047></A> if (status != 0) {
<A name=40048></A> printf("Error using matPutVariableAsGlobal\n");
<A name=40049></A> return(EXIT_FAILURE);
<A name=40050></A> }
<A name=40051></A>
<A name=40052></A> status = matPutVariable(pmat, "LocalString", pa3);
<A name=40053></A> if (status != 0) {
<A name=40054></A> printf("%s : Error using matPutVariable on line %d\n",
__FILE__, __LINE__);
<A name=40055></A> return(EXIT_FAILURE);
<A name=40056></A> }
<A name=40057></A>
<A name=40058></A> /*
<A name=40059></A> * Ooops! we need to copy data before writing the array. (Well,
<A name=40060></A> * ok, this was really intentional.) This demonstrates that
<A name=40061></A> * matPutVariable will overwrite an existing array in a
MAT-file.
<A name=40062></A> */
<A name=40063></A> memcpy((void *)(mxGetPr(pa1)), (void *)data, sizeof(data));
<A name=40064></A> status = matPutVariable(pmat, "LocalDouble", pa1);
<A name=40065></A> if (status != 0) {
<A name=40066></A> printf("%s : Error using matPutVariable on line %d\n",
__FILE__, __LINE__);
<A name=40067></A> return(EXIT_FAILURE);
<A name=40068></A> }
<A name=40069></A>
<A name=40070></A> /* Clean up. */
<A name=40071></A> mxDestroyArray(pa1);
<A name=40072></A> mxDestroyArray(pa2);
<A name=40073></A> mxDestroyArray(pa3);
<A name=40074></A>
<A name=40075></A> if (matClose(pmat) != 0) {
<A name=40076></A> printf("Error closing file %s\n",file);
<A name=40077></A> return(EXIT_FAILURE);
<A name=40078></A> }
<A name=40079></A>
<A name=40080></A> /* Re-open file and verify its contents with matGetVariable. */
<A name=40083></A> pmat = matOpen(file, "r");
<A name=40084></A> if (pmat == NULL) {
<A name=40085></A> printf("Error reopening file %s\n", file);
<A name=40086></A> return(EXIT_FAILURE);
<A name=40087></A> }
<A name=40088></A>
<A name=40089></A> /* Read in each array we just wrote. */
<A name=40092></A> pa1 = matGetVariable(pmat, "LocalDouble");
<A name=40093></A> if (pa1 == NULL) {
<A name=40094></A> printf("Error reading existing matrix LocalDouble\n");
<A name=40095></A> return(EXIT_FAILURE);
<A name=40096></A> }
<A name=40097></A> if (mxGetNumberOfDimensions(pa1) != 2) {
<A name=40098></A> printf("Error saving matrix: result does not have two
dimensions\n");
<A name=40099></A> return(EXIT_FAILURE);
<A name=40100></A> }
<A name=40101></A>
<A name=40102></A> pa2 = matGetVariable(pmat, "GlobalDouble");
<A name=40103></A> if (pa2 == NULL) {
<A name=40104></A> printf("Error reading existing matrix GlobalDouble\n");
<A name=40105></A> return(EXIT_FAILURE);
<A name=40106></A> }
<A name=40107></A> if (!(mxIsFromGlobalWS(pa2))) {
<A name=40108></A> printf("Error saving global matrix: result is not global\n");
<A name=40109></A> return(EXIT_FAILURE);
<A name=40110></A> }
<A name=40111></A>
<A name=40112></A> pa3 = matGetVariable(pmat, "LocalString");
<A name=40113></A> if (pa3 == NULL) {
<A name=40114></A> printf("Error reading existing matrix LocalString\n");
<A name=40115></A> return(EXIT_FAILURE);
<A name=40116></A> }
<A name=40117></A>
<A name=40118></A> status = mxGetString(pa3, str, sizeof(str));
<A name=40119></A> if(status != 0) {
<A name=40120></A> printf("Not enough space. String is truncated.");
<A name=40121></A> return(EXIT_FAILURE);
<A name=40122></A> }
<A name=40123></A> if (strcmp(str, "MATLAB: the language of technical
computing")) {
<A name=40124></A> printf("Error saving string: result has incorrect
contents\n");
<A name=40125></A> return(EXIT_FAILURE);
<A name=40126></A> }
<A name=40127></A>
<A name=40128></A> /* Clean up before exit. */
<A name=40129></A> mxDestroyArray(pa1);
<A name=40130></A> mxDestroyArray(pa2);
<A name=40131></A> mxDestroyArray(pa3);
<A name=40132></A>
<A name=40133></A> if (matClose(pmat) != 0) {
<A name=40134></A> printf("Error closing file %s\n",file);
<A name=40135></A> return(EXIT_FAILURE);
<A name=40136></A> }
<A name=40137></A> printf("Done\n");
<A name=40138></A> return(EXIT_SUCCESS);
<A name=40139></A>}
</PRE></LI></UL>
<P><A name=31557></A>To produce an executable version of this example program,
compile the file and link it with the appropriate library. Details on how to
compile and link MAT-file programs on the various platforms are discussed in the
section, <A
href="http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/ch05ma12.shtml#19027">Compiling
and Linking MAT-File Programs</A>.</P>
<P><A name=19218></A>Once you have compiled and linked your MAT-file program,
you can run the stand-alone application you have just produced. This program
creates a MAT-file, <CODE>mattest.mat</CODE>, that can be loaded into MATLAB. To
run the application, depending on your platform, either double-click on its icon
or enter <CODE>matcreat</CODE> at the system prompt.</P>
<UL class=continued>
<LI class=continued><PRE><A name=28080></A>matcreat
<A name=28077></A>Creating file mattest.mat...
</PRE></LI></UL>
<P><A name=26594></A>To verify that the MAT-file has been created, at the MATLAB
prompt enter</P>
<UL class=continued>
<LI class=continued><PRE><A name=26596></A>whos -file mattest.mat
<A name=26597></A> Name Size Bytes Class
<A name=26598></A>
<A name=26599></A> GlobalDouble 3x3 72 double array (global)
<A name=26600></A> LocalDouble 3x3 72 double array
<A name=26601></A> LocalString 1x43 86 char array
<A name=26602></A>
<A name=26603></A>Grand total is 61 elements using 230 bytes
</PRE></LI></UL>
<P><BR>
<P>
<TABLE bgColor=#d9e1e1 border=0 cellPadding=0 cellSpacing=0 width="100%">
<TBODY>
<TR vAlign=top>
<TD align=left width=20><A
href="http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/ch05mat7.shtml"><IMG
align=bottom border=0
src="Creating a MAT-File in C.files/b_prev.gif"></A> </TD>
<TD align=left> Finding Associated Files</TD>
<TD> </TD>
<TD align=right>Reading a MAT-File in C </TD>
<TD align=right width=20><A
href="http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/ch05mat9.shtml"><IMG
align=bottom border=0
src="Creating a MAT-File in C.files/b_next.gif"></A></TD></TR></TBODY></TABLE><BR><!-- Copyright 2002 The MathWorks, Inc. --><!-- Last updated: Thu Jun 20 22:04:48 2002 --><BR>
<TABLE border=0 cellPadding=0 cellSpacing=0 width=459>
<TBODY>
<TR>
<TD><IMG alt="" height=1
src="Creating a MAT-File in C.files/pixelclear.gif" width=8></TD>
<TD align=right style="FONT-FAMILY: arial, sans-serif; FONT-SIZE: 8pt"
vAlign=top><IMG alt="" border=0 height=3
src="Creating a MAT-File in C.files/tmline.gif" width=451><BR><IMG
alt="Copyright 1994-2002 by The MathWorks, Inc."
src="Creating a MAT-File in C.files/copyright.gif"> 1994-2002 The
MathWorks, Inc. <A
href="http://www.mathworks.com/company/trademarks.shtml"
style="FONT-FAMILY: arial, sans-serif; FONT-SIZE: 8pt">Trademarks</A>
<A
href="http://www.mathworks.com/company/policies/privacypolicy.shtml"
style="FONT-FAMILY: arial, sans-serif; FONT-SIZE: 8pt">Privacy
Policy</A></TD></TR></TBODY></TABLE></P></BODY></HTML>
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -