?? reading 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/ch05mat9.shtml -->
<HTML><HEAD><TITLE>Importing and Exporting Data (External Interfaces/API)</TITLE>
<META content="text/html; charset=gb2312" http-equiv=Content-Type><LINK
href="Reading a MAT-File in C.files/docset.css" rel=stylesheet type=text/css><!-- $Revision$ $Date$ --><!-- DOCNAME: External Interfaces/API --><!-- CHUNKNAME: Reading a MAT-File in C --><!-- 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/ch05mat8.shtml"><IMG
border=0
src="Reading a MAT-File in C.files/b_prev.gif"></A> <A
href="http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/ch05ma10.shtml"><IMG
border=0
src="Reading a MAT-File in C.files/b_next.gif"></A></TD></TR></TBODY></TABLE><A
name=reading_a_mat-file_in_c></A><!-- H2 --><A name=32276></A>
<P><FONT class=Head3 size=+1><B>Reading a MAT-File in C</B></FONT><BR>
<P><A name=32278></A>This sample program illustrates how to use the library
routines to read and diagnose a MAT-file.</P>
<UL class=continued>
<LI class=continued><PRE><A name=32280></A>/*
<A name=32281></A> * MAT-file diagnose program
<A name=32282></A> *
<A name=32283></A> * Calling syntax:
<A name=32284></A> *
<A name=32285></A> * matdgns <matfile.mat>
<A name=32286></A> *
<A name=32287></A> * It diagnoses the MAT-file named <matfile.mat>.
<A name=32288></A> *
<A name=32289></A> * This program demonstrates the use of the following functions:
<A name=32290></A> *
<A name=32291></A> * matClose
<A name=32292></A> * matGetDir
<A name=32293></A> * matGetNextVariable
<A name=32294></A> * matGetNextVariableInfo
<A name=32295></A> * matOpen
<A name=32296></A> *
<A name=32297></A> * Copyright (c) 1984-2000 The MathWorks, Inc.
<A name=40201></A> * $Revision: 1.9 $
<A name=32298></A> */
<A name=32299></A>
<A name=40213></A>#include <stdio.h>
<A name=40214></A>#include <stdlib.h>
<A name=40215></A>#include "mat.h"
<A name=40216></A>
<A name=40217></A>int diagnose(const char *file) {
<A name=40218></A> MATFile *pmat;
<A name=40219></A> char **dir;
<A name=40220></A> const char *name;
<A name=40221></A> int ndir;
<A name=40222></A> int i;
<A name=40223></A> mxArray *pa;
<A name=40224></A>
<A name=40225></A> printf("Reading file %s...\n\n", file);
<A name=40226></A>
<A name=40227></A> /* Open file to get directory. */
<A name=40230></A> pmat = matOpen(file, "r");
<A name=40231></A> if (pmat == NULL) {
<A name=40232></A> printf("Error opening file %s\n", file);
<A name=40233></A> return(1);
<A name=40234></A> }
<A name=40235></A>
<A name=40236></A> /* Get directory of MAT-file. */
<A name=40239></A> dir = matGetDir(pmat, &ndir);
<A name=40240></A> if (dir == NULL) {
<A name=40241></A> printf("Error reading directory of file %s\n", file);
<A name=40242></A> return(1);
<A name=40243></A> } else {
<A name=40244></A> printf("Directory of %s:\n", file);
<A name=40245></A> for (i=0; i < ndir; i++)
<A name=40246></A> printf("%s\n", dir[i]);
<A name=40247></A> }
<A name=40248></A> mxFree(dir);
<A name=40249></A>
<A name=40250></A> /* In order to use matGetNextXXX correctly, reopen file to
read in headers. */
<A name=40251></A> if (matClose(pmat) != 0) {
<A name=40252></A> printf("Error closing file %s\n",file);
<A name=40253></A> return(1);
<A name=40254></A> }
<A name=40255></A> pmat = matOpen(file, "r");
<A name=40256></A> if (pmat == NULL) {
<A name=40257></A> printf("Error reopening file %s\n", file);
<A name=40258></A> return(1);
<A name=40259></A> }
<A name=40260></A>
<A name=40261></A> /* Get headers of all variables. */
<A name=40262></A> printf("\nExamining the header for each variable:\n");
<A name=40263></A> for (i=0; i < ndir; i++) {
<A name=40264></A> pa = matGetNextVariableInfo(pmat, &name);
<A name=40265></A> if (pa == NULL) {
<A name=40266></A> printf("Error reading in file %s\n", file);
<A name=40267></A> return(1);
<A name=40268></A> }
<A name=40269></A> /* Diagnose header pa. */
<A name=40270></A> printf("According to its header, array %s has %d
dimensions\n",
<A name=40271></A> name, mxGetNumberOfDimensions(pa));
<A name=40272></A> if (mxIsFromGlobalWS(pa))
<A name=40273></A> printf(" and was a global variable when saved\n");
<A name=40274></A> else
<A name=40275></A> printf(" and was a local variable when saved\n");
<A name=40276></A> mxDestroyArray(pa);
<A name=40277></A> }
<A name=40278></A>
<A name=40279></A> /* Reopen file to read in actual arrays. */
<A name=40280></A> if (matClose(pmat) != 0) {
<A name=40281></A> printf("Error closing file %s\n",file);
<A name=40282></A> return(1);
<A name=40283></A> }
<A name=40284></A> pmat = matOpen(file, "r");
<A name=40285></A> if (pmat == NULL) {
<A name=40286></A> printf("Error reopening file %s\n", file);
<A name=40287></A> return(1);
<A name=40288></A> }
<A name=40289></A>
<A name=40290></A> /* Read in each array. */
<A name=40291></A> printf("\nReading in the actual array contents:\n");
<A name=40292></A> for (i=0; i<ndir; i++) {
<A name=40293></A> pa = matGetNextVariable(pmat, &name);
<A name=40294></A> if (pa == NULL) {
<A name=40295></A> printf("Error reading in file %s\n", file);
<A name=40296></A> return(1);
<A name=40297></A> }
<A name=40298></A> /*
<A name=40299></A> * Diagnose array pa
<A name=40300></A> */
<A name=40301></A> printf("According to its contents, array %s has %d
dimensions\n", name, mxGetNumberOfDimensions(pa));
<A name=40303></A> if (mxIsFromGlobalWS(pa))
<A name=40304></A> printf(" and was a global variable when saved\n");
<A name=40305></A> else
<A name=40306></A> printf(" and was a local variable when saved\n");
<A name=40307></A> mxDestroyArray(pa);
<A name=40308></A> }
<A name=40309></A>
<A name=40310></A> if (matClose(pmat) != 0) {
<A name=40311></A> printf("Error closing file %s\n",file);
<A name=40312></A> return(1);
<A name=40313></A> }
<A name=40314></A> printf("Done\n");
<A name=40315></A> return(0);
<A name=40316></A>}
<A name=40317></A>
<A name=40318></A>
<A name=40442></A>int main(int argc, char **argv)
<A name=40319></A>{
<A name=40321></A> int result;
<A name=40322></A>
<A name=40323></A> if (argc > 1)
<A name=40324></A> result = diagnose(argv[1]);
<A name=40325></A> else{
<A name=40326></A> result = 0;
<A name=40327></A> printf("Usage: matdgns <matfile>");
<A name=40328></A> printf(" where <matfile> is the name of the MAT-file");
<A name=40329></A> printf(" to be diagnosed\n");
<A name=40330></A> }
<A name=40332></A> return (result==0) ? EXIT_SUCCESS : EXIT_FAILURE;
<A name=40334></A>}
</PRE></LI></UL>
<P><A name=33243></A>After compiling and linking this program, you can view its
results.</P>
<UL class=continued>
<LI class=continued><PRE><A name=32421></A>matdgns mattest.mat
<A name=32422></A>Reading file mattest.mat...
<A name=32423></A>
<A name=32424></A>Directory of mattest.mat:
<A name=32425></A>GlobalDouble
<A name=32426></A>LocalString
<A name=32427></A>LocalDouble
<A name=32428></A>
<A name=32429></A>Examining the header for each variable:
<A name=32430></A>According to its header, array GlobalDouble has 2 dimensions
<A name=32431></A> and was a global variable when saved
<A name=32432></A>According to its header, array LocalString has 2 dimensions
<A name=32433></A> and was a local variable when saved
<A name=32434></A>According to its header, array LocalDouble has 2 dimensions
<A name=32435></A> and was a local variable when saved
<A name=32436></A>
<A name=32437></A>Reading in the actual array contents:
<A name=32438></A>According to its contents, array GlobalDouble has 2 dimensions
<A name=32439></A> and was a global variable when saved
<A name=32440></A>According to its contents, array LocalString has 2 dimensions
<A name=32441></A> and was a local variable when saved
<A name=32442></A>According to its contents, array LocalDouble has 2 dimensions
<A name=32443></A> and was a local variable when saved
<A name=32444></A>Done
</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/ch05mat8.shtml"><IMG
align=bottom border=0
src="Reading a MAT-File in C.files/b_prev.gif"></A> </TD>
<TD align=left> Examples of MAT-Files</TD>
<TD> </TD>
<TD align=right>Creating a MAT-File in Fortran </TD>
<TD align=right width=20><A
href="http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/ch05ma10.shtml"><IMG
align=bottom border=0
src="Reading 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="Reading 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="Reading a MAT-File in C.files/tmline.gif" width=451><BR><IMG
alt="Copyright 1994-2002 by The MathWorks, Inc."
src="Reading 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>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -