Using assignin or evalin command to populate Structure Elements with Numeric Data.
5 次查看(过去 30 天)
显示 更早的评论
Hello,
I am getting an error while executing the below command:
assignin('base', 'A.B', 1)|
Error:
??? Error using ==> assignin
Invalid variable name "A.B" in ASSIGNIN.
As a work around i tried to use:
assignin('base', 'temp', 1);
evalin('base','A.B = temp');
evalin('base','clear temp');
The above work around was proposed in one of the previous threads:
The problem I have is that, my Structure elements are read from a Cell Array inside a for loop.
Lets say C = {'A.B', 'A1.B1'}
So how do I pass 'C' as a parameter to the below line:
evalin('base','C = temp')
Expected output: A.B = 1, A1.B1 = 1 (In Base Workspace)
I also tried to use setfield command, but could not succeed.
Thanks, Shyama
1 个评论
采纳的回答
Azzi Abdelmalek
2015-6-24
编辑:Azzi Abdelmalek
2015-6-24
You probably can use other alternatives here http://www.mathworks.com/help/matlab/matlab_prog/string-evaluation.html
For your problem you can use
C = {'A.B', 'A1.B1'}
assignin('base', 'temp', 1);
cellfun(@(x) evalin('base',[x '=temp']),C)
更多回答(1 个)
Matt J
2015-6-24
Not sure why you're not just using setfield(), but another option is
evalin('base','A.B=1');
or
evalin('base',['A.B=' num2str(1)]);
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!