How to save workspace variables in a cell array at specific location (Example: C{1,1}(1,1)) in the same workspace?

I want to save a workspace variable at location C{1,1}(1,1) in a cell array which is present in the same workspace. I used this command. It executes in command window but the same logic fails in editor window when written at button callback.
M = 1:10;
M = M';
C = {M};
assignin('base','C',C);
C{1,1}(1,1) = 55;

3 Comments

Note that assignin is the least recommended way of passing data between callbacks:
Using assignin makes your work harder to debug, as you are now finding out. Much better would be to use the handles structure, guidata, or nested functions.
Can you please tell me how can i save value at a specific location in a cell array. this only executes in command window but not in editor
c{1,1}(1,1) = 42;
@Tanzeela Javid: and what happens when you try to execute that line? Do you get a warning, or an error, or the wrong value, or something else...? If you get any warning/error message then please show us the complete message. If it runs but gives an incorrect value (or something else...) then please tell us what you expect and what it does.

Sign in to comment.

Answers (2)

This is the error msg when I execute this code.
x=evalin('base',value1); y=evalin('base', value2); assignin('base',C{1,1}(1,1),x); assignin('base',C{1,1}(2,1),y);

4 Comments

Please do not post details of the question in the section for answer. I have problem zoominh into the screen shot. Prefer to post error messages as text and format your code using the "{} Code" button. This helps the readers to understand your problem.
Please read the documentation of assignin: doc assignin. The 2nd input must be a string, namely the variable to be assigned.
Do not try to change elements of a cell remotely. Such indirect programming methods impedes the debugging. Better create the variables inside the GUI, see the links posted by Stephen.
Error using assignin Unknown command option.
Error in int2>pushbutton1_Callback (line 496) assignin('base',C{1,1}(1,1),x);
Error in gui_mainfcn (line 95) feval(varargin{:});
Error in int2 (line 42) gui_mainfcn(gui_State, varargin{:});
Error in matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)int2('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback
I dont understand how to share data between callbacks
@Tanzeela Javid: I gave you some links to look at. Personally I think using nested functions is a very easy way to share data between callbacks. Give them a try.

Sign in to comment.

%reading value from workspace
x = evalin('base','value1');
y = evalin('base','value2');
assignin('base',C{1,1}(1,1),x);
assignin('base',C{1,1}(2,1),y)

Asked:

N/A
on 4 May 2017

Commented:

on 5 May 2017

Community Treasure Hunt

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

Start Hunting!