MATLAB: Accessing and editing workspace values in matlab code

variableworkspace

I need to search a variable in workspace and edit its value at some specific index. How do i access, edit that variable and save new value back to workspace. Thanks,

Best Answer

You code looks correct so far (little typo in the assignin command, it must be array instead of arraya). But I think this should be faster since you have to copy one value only:
function test(mystruct)
for i=1:1
assignin('base','tmp',mystruct(i).value)
cmd_string = [mystruct(i).name,'(',num2str(mystruct(i).rowIndex),',',num2str(mystruct(i).colIndex),')=tmp;clear tmp'];
evalin('base',cmd_string)
end
end
I tested it with:
a = [1 2 3 4]
mystruct.name = 'a'
mystruct.rowIndex = 1;
mystruct.colIndex = 1;
mystruct.value = 14;
test(mystruct)