Accessing and editing workspace values in matlab code

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,

 Respuesta aceptada

Friedrich
Friedrich el 8 de Jul. de 2011
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)

Más respuestas (2)

Friedrich
Friedrich el 8 de Jul. de 2011
I think what you are looking for is the evalin command. So something like
evalin('base','a=3')
Or if you really have to search you can do:
var = evalin('base','whos;')

6 comentarios

Sadanand
Sadanand el 8 de Jul. de 2011
Thnks for ur response.
I can get workspace variable with evalin command but then i need to modify some value of that variable.
Lets say that workspace variable is array. i want to access that variable in my code and modify its value at specific index.
Please help.
Friedrich
Friedrich el 8 de Jul. de 2011
You can do it this way:
%base workspace
base_array = 1:10;
%do this in function
array = evalin('base','base_array;')
array(3) = 37;
assignin('base','base_array',array)
But this will copy the full array everytime.
Friedrich
Friedrich el 8 de Jul. de 2011
Or when single value is needed:
array_at_index = evalin('base','base_array(3);')
array_at_index = array_at_index * 2;
assignin('base','tmp',array_at_index)
evalin('base','base_array(3) = tmp;clear tmp;')
Sadanand
Sadanand el 8 de Jul. de 2011
Name of the workspace variable is present in some other variable. Lets say workspace variable name is "ABC" and it is present in variable x.
So when i write evalin('base', x) it throws error. I have to give second parameter in single quotes. When i write evalin('base', 'x'), it searches for x instead of ABC. Any solution would be appreciated.
Friedrich
Friedrich el 8 de Jul. de 2011
Sure evalin('base',x) throws an error since x is not present it the ML workspace. you have to ways here how to proceed and both are stated above:
1.) assignin('base','ABC',x) %will overwrite variable ABC with the values of x
2.)assignin('base','x',x) %copy the variable in the ML Workspace
evalin('base','ABC(some_index) = x; clear x')
Sadanand
Sadanand el 8 de Jul. de 2011
Sorry to borrow your time again. Here is my actual problem. I have function that takes array of structure. Structure has following fields.
name, rowIndex, columnIndex, value. For each structure in the structure array, i need to get struct.name and search it in workspace and replace value of this workspace variable with struct.value at index struct.rowIndex and struct.colIndex.
Here is what i have written.
function myFuction(mystruct)
for i=1: 2
array = evalin('base', mystruct(i).name);
array(mystruct(i).rowIndex, mystruct(i).colIndex) = mystruct(i).value;
assignin('base', mystruct(i).name, arraya);
clear array;
end
end
Thank You for your help.

Iniciar sesión para comentar.

yasser
yasser el 11 de Nov. de 2013
am looking for value in some workspaces, can i get name of workspace and index of [row,col]? how to do that?

Categorías

Más información sobre Data Import and Export en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 8 de Jul. de 2011

Respondida:

el 11 de Nov. de 2013

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by