For Programmers: Free Programming Magazines  


Home > Archive > Matlab > November 2005 > How do I erase a variable?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How do I erase a variable?
Yoav

2005-11-29, 7:07 pm

I need a function that (among other things) erases the object that it
got as a parameter:
something like this:

function a=temp(a)
a = [];

and then:
b = [4 5];
b(1) = temp(b(1));

this doesn't work...
Can anyone help me please?

Thanks!
Y.
Jérôme

2005-11-29, 7:07 pm

Hi,

b = [4 5]
b(1) = [];
b

Jérôme
Yoav

2005-11-29, 7:07 pm

Yes, well, I would have done it if I could but I need it to be a
function that does that.

I want this function to erase whatever object it got.
This is not possible?
I will have to do that outside the function?
(like I'm already doing now?)

Thank you for now.
Y.

Jérôme wrote:
>
>
> Hi,
>
> b = [4 5]
> b(1) = [];
> b
>
> Jérôme

Jérôme

2005-11-29, 7:07 pm

Yoav wrote:

> to erase whatever object it got.


What do you mean ?

Jérôme
Jos

2005-11-29, 7:07 pm

Yoav wrote:[color=darkred]
>
>
> Yes, well, I would have done it if I could but I need it to be a
> function that does that.
>
> I want this function to erase whatever object it got.
> This is not possible?
> I will have to do that outside the function?
> (like I'm already doing now?)
>
> Thank you for now.
> Y.
>
> Jérôme wrote:

take a look at <inputname> and <evalin>

Jos
dario

2005-11-29, 7:07 pm


try:
eraseVar=@(myVar) (assignin('caller',inputname(1), []))

Maybe using sprintf you can build a string like
' clear myVarName'
( where myVarName is the name of the var. to delete)
and evaluate it in the base (or caller) workspace

Randy Poe

2005-11-29, 7:07 pm


Yoav wrote:
> I need a function that (among other things) erases the object that it
> got as a parameter:
> something like this:
>
> function a=temp(a)
> a = [];
>
> and then:
> b = [4 5];
> b(1) = temp(b(1));
>
> this doesn't work...


It gives me this error:

??? In an assignment A(I) = B, the number of elements in B and
I must be the same.

It didn't like b(1) = (empty result of a function call) even though
b(1) = [] works fine.

I tried a few different things. I thought I could use INPUTNAME,
but that won't give you the string 'b(1)' if b(1) is the argument.

Here's the cutest solution I came up with:

function temp(a)
evalin('caller', [a '=[];']);
[color=darkred]
b =
4 5[color=darkred]
b =
5[color=darkred]
a =
3 4 5[color=darkred]
a =
[]

I'm using the trick that the command line
temp b(1)
is equivalent to
temp('b(1)')
i.e. it passes the string, not the variable itself.

- Randy

Yoav

2005-11-29, 7:07 pm

This is more complicated than I thought it would be... I thought I
could just make the function assign a [] value.

I'll check out your solutions in depth.

Thank you.
Y.

dario wrote:
>
>
>
> try:
> eraseVar=@(myVar) (assignin('caller',inputname(1), []))
>
> Maybe using sprintf you can build a string like
> ' clear myVarName'
> ( where myVarName is the name of the var. to delete)
> and evaluate it in the base (or caller) workspace
>
>

Yoav

2005-11-29, 7:07 pm

Randy,

That's a perfect solution!
Only problem (tear) is that I cannot have a string as an input
parameter because it's a class and all functions must have the object
as the input argument and not a string.

I'll try and take your advices and think of an elegant way to do it
yet.

Thank you again.
Yoav.

Randy Poe wrote:
>
>
>
> Yoav wrote:
> that it
>
> It gives me this error:
>
> ??? In an assignment A(I) = B, the number of elements in B and
> I must be the same.
>
> It didn't like b(1) = (empty result of a function call) even though
> b(1) = [] works fine.
>
> I tried a few different things. I thought I could use INPUTNAME,
> but that won't give you the string 'b(1)' if b(1) is the argument.
>
> Here's the cutest solution I came up with:
>
> function temp(a)
> evalin('caller', [a '=[];']);
>
> b =
> 4 5
> b =
> 5
> a =
> 3 4 5
> a =
> []
>
> I'm using the trick that the command line
> temp b(1)
> is equivalent to
> temp('b(1)')
> i.e. it passes the string, not the variable itself.
>
> - Randy
>
>

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com