Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to delete variables in "matlab.mat"

715 views
Skip to first unread message

Daniele Barile

unread,
Aug 19, 1998, 3:00:00 AM8/19/98
to
Hi everyone.

I'm a new member of this group.
I need to know how to delete some variables saved by
SAVE
in the file "matlab.mat".

Thanks in advance.

Daniele

Daniele Barile

unread,
Aug 19, 1998, 3:00:00 AM8/19/98
to

Daniele Barile

unread,
Aug 19, 1998, 3:00:00 AM8/19/98
to

SGJ4ASIT

unread,
Aug 19, 1998, 3:00:00 AM8/19/98
to
>Hi everyone.
>
>I'm a new member of this group.
>I need to know how to delete some variables saved by
> SAVE
>in the file "matlab.mat".

if you're talking about deleting the variables in your workspace,
see the help for clear (type "help clear" at the command line).

example: you've saved the variables "me" and "you" in matlab.mat.
to delete them from your workspace, type "clear me you" (leave out
the quotes) at the command line.

if you're talking about not saving certain variables in the matfile,
see the help for save (type "help save" at the command line). this
will also tell you how to give the file a name of your choice, rather
than the default matlab.mat.

example: you have variables named "me" and "you" in your workspace.
you can save just the variable "me" in the file daniele.mat by typing
save('daniele.mat', 'me') at the command line.

>Thanks in advance.
>
>Daniele

--
there is water!
at the bottom of the ocean!

- talking heads


raymond s. norris

unread,
Aug 24, 1998, 3:00:00 AM8/24/98
to
[This followup was posted to comp.soft-sys.matlab and a copy was sent to
the cited author.]

hi daniele-

i don't see a way to do it as an M-file, but this has MEX-file written
all over it. compile the following code as such:

>> mex delvar.c

then you can run as:

>> delvar <name_of_matfile> <name_of_variable_to_delete>

this function requires the user to list the name of the matfile, but you
could code it otherwise to look for matlab.mat if a filename was not
given.

also, this function only deletes one variable per call, but you could
easily delete more by looping through the remaining variable names on the
argument list.

to find out more about MEX-files, check out the "MATLAB Application
Program Interface Guide" version 5.

hth,
-raymond

---< delvar.c >---

/*
* file : delvar.c
*
* delvar MAT-file variable
*/

#include "mat.h"
#include "mex.h"

/*
* usage of MEX-file
*/
void
printUsage()
{
mexPrintf("Usage: %s MAT-file variable\n",mexFunctionName());
}

void
mexFunction( int nlhs,
mxArray *plhs[],
int nrhs,
const mxArray *prhs[]
)
{
MATFile *mfp;
mxArray *status;
char *filename;
char *variable;
int buffersize;
int status;

/*
* the value returned by the left-hand side is the status. return 1 if
* there is a failure. return 0 if there is success (variable was
* removed from MAT-file.
*/
plhs[0]=mxCreateDoubleMatrix(1,1,mxREAL);
mxGetPr(plhs)[0] = 1;

/*
* error checking for input arguments
*/
if (nrhs!=2) {
printUsage();
return;
}

if ( (!mxIsChar(prhs[0])) ||
(!mxIsChar(prhs[1])) ) {
printUsage();
return;
}

/*
* get filename to open
*/
buffersize=mxGetM(prhs[0])*mxGetN(prhs[0])+1;
filename=mxCalloc(buffersize,sizeof(char));
mxGetString(prhs[0],filename,buffersize);

/*
* open MAT-file
*/
mfp=matOpen(filename,"u+");
if (mfp==(MATFile *)NULL) {
mexPrintf("Error: Failed to open file '%s'.\n",filename);
mxFree(filename);
return;
}

/*
* get variable to delete
*/
buffersize=mxGetM(prhs[1])*mxGetN(prhs[1])+1;
variable=mxCalloc(buffersize,sizeof(char));
mxGetString(prhs[1],variable,buffersize);

/*
* delete variable from MAT-file
*/
status=matDeleteArray(mfp,variable);
if (status!=0) {
mexPrintf("Error: Failed to delete variable '%s'.\n",variable);
mxFree(filename);
mxFree(variable);
return;
}

/*
* cleanup
*/
mxFree(filename);
mxFree(variable);
matClose(mfp);

/*
* change return status to success
*/
mxGetPr(plhs)[0]=0;
}

---< delvar.c >---


In article <6rekfl$4ig$1...@server-b.cs.interbusiness.it>,
dba...@cerreto.peoples.it says...


> Hi everyone.
>
> I'm a new member of this group.
> I need to know how to delete some variables saved by
> SAVE
> in the file "matlab.mat".
>

> Thanks in advance.
>
> Daniele
>
>
>
>
>
>
>

Damian T. Packer

unread,
Aug 25, 1998, 3:00:00 AM8/25/98
to
how about making an MFile function named delvar (thanks to Raymond for the
name):

function delvar(a_very_unlikely_varname)

load matlab.mat

clear(a_very_unlikely_varname)
clear a_very_unlikely_varname

save matlab.mat

Note that this will have the side-effect of clearing any variable in the
matfile named "a_very_unlikely_varname", but I'd say the odds of that not
happening are in your favor.

Does this help?

-Damian


Daniele Barile wrote in message
<6reghe$st4$1...@server-b.cs.interbusiness.it>...

0 new messages