|
|
Assignin into Matlab variable struct
I would like to do such thing :
[code]
PARAMS={'struct.x',[1 2 3]};
for i=1:length(PARAMS{2})
assignin('base',PARAMS{1},PARAMS{2}(i));
end
[\code]
The user should input the name of the variable that would be changed into the loop as a first field in the PARAMS cell array.The different values of the variable are given into an array.
But MatLab give me the following error:
??? Error using ==> assignin
Invalid variable name "struct.x" in ASSIGNIN.
Apparently the assignin function cannot be used to assign values into struct fields. Is there another function or a better way to do such thing ?
Thanks in advance.
|
|
0
|
|
|
|
Reply
|
Sébastien
|
12/26/2009 9:50:20 AM |
|
"Sébastien " <sebastien.grolleau.removethis@cea.fr> wrote in message
news:hh4m8s$2lj$1@fred.mathworks.com...
>I would like to do such thing :
>
> [code]
> PARAMS={'struct.x',[1 2 3]};
>
> for i=1:length(PARAMS{2})
> assignin('base',PARAMS{1},PARAMS{2}(i));
> end
> [\code]
>
> The user should input the name of the variable that would be changed into
> the loop as a first field in the PARAMS cell array.The different values of
> the variable are given into an array.
> But MatLab give me the following error: ??? Error using ==> assignin
> Invalid variable name "struct.x" in ASSIGNIN.
>
> Apparently the assignin function cannot be used to assign values into
> struct fields.
That's correct. The second input to the ASSIGNIN function must be a
_variable name_, not an _expression_ like 'struct.x'.
> Is there another function or a better way to do such thing ?
Why are you trying to create a struct array in the base workspace from
within your script or function?
My suggestion for "a better way to do such thing" is "find a way to avoid
having to create such a variable", but that's just me.
If you _MUST_ do so, construct the structure inside your function and assign
it into the struct array in the base workspace.
assignin('base', 'struct', struct('x', 1));
But two things:
1) This "poofs" a variable into the base workspace -- all the Bad Things
that we've discussed on this newsgroup in the past with regards to poofing
variables applies in this case.
2) Don't create a variable named struct in any workspace if you want to be
able to use the STRUCT _function_ that creates a struct array -- use a
different name.
--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
|
|
0
|
|
|
|
Reply
|
Steven
|
1/1/2010 3:12:18 AM
|
|
"Sébastien " <sebastien.grolleau.removethis@cea.fr> wrote in message <hh4m8s$2lj$1@fred.mathworks.com>...
> I would like to do such thing :
>
> [code]
> PARAMS={'struct.x',[1 2 3]};
>
> for i=1:length(PARAMS{2})
> assignin('base',PARAMS{1},PARAMS{2}(i));
> end
> [\code]
====================
Even if it were possible, the for-loop you've written here would seem to overwrite struct.x 3 times successively with different values. In other words, it would be equivalent to the one-liner
assignin('base',PARAMS{1},PARAMS{2}(end));
==========================
> The user should input the name of the variable that would be changed into the loop as a first field in the PARAMS cell array.The different values of the variable are given into an array.
>
> But MatLab give me the following error:
> ??? Error using ==> assignin
> Invalid variable name "struct.x" in ASSIGNIN.
>
> Apparently the assignin function cannot be used to assign values into struct fields. Is there another function or a better way to do such thing ?
===========================
Perhaps the setfield() command is what you're looking for?
|
|
0
|
|
|
|
Reply
|
Matt
|
1/1/2010 6:25:06 AM
|
|
|
2 Replies
1182 Views
(page loaded in 0.157 seconds)
|
|
|
|
|
|
|
|
|