Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

using load in nested fuctions error

319 views
Skip to first unread message

bart Zaalberg

unread,
Feb 23, 2010, 11:57:02 AM2/23/10
to
hi guys,

im quite new to matlab but trying to make a little game in matlab. when i started i used a new m file every time i made a new part. now i'm trying to put them all in 1 m-file using nested functions. the probleme is that i used load/save to store my information so you could continue an other time, but now it gives the next error(when combined in 1 m-file:

??? Error using ==> load
Attempt to add "keuzem" to a static workspace.
See MATLAB Programming, Restrictions on Assigning to Variables for
details.

Error in ==> RPGfinal at 7
load('BartRPG.mat')

i know i get a error because its i a static workspace but isn't there any way i can save my varaibles and load then an other time. predesignating then before loading also doesn't work.

i will post the m-file if its nessesary,

hope someone can help me.

bart,
Student

PS. please dont mind my english i'm living in the netherlands.

bart Zaalberg

unread,
Feb 23, 2010, 12:04:05 PM2/23/10
to
a little bit more information.

i can load the .mat file in my main function but not in the nested, so question is; is there a way to load a .mat file in a nested fuction?

sorry for the extra post

Steven Lord

unread,
Feb 23, 2010, 1:26:32 PM2/23/10
to

"bart Zaalberg" <b.zaa...@student.utwente.nl> wrote in message
news:hm11cu$hpf$1...@fred.mathworks.com...

> hi guys,
>
> im quite new to matlab but trying to make a little game in matlab. when i
> started i used a new m file every time i made a new part. now i'm trying
> to put them all in 1 m-file using nested functions. the probleme is that i
> used load/save to store my information so you could continue an other
> time, but now it gives the next error(when combined in 1 m-file:
>
> ??? Error using ==> load
> Attempt to add "keuzem" to a static workspace.
> See MATLAB Programming, Restrictions on Assigning to Variables for
> details.
>
> Error in ==> RPGfinal at 7
> load('BartRPG.mat')

That is correct. You can't "poof" variables into the workspace of a nested
function like this.

> i know i get a error because its i a static workspace but isn't there any
> way i can save my varaibles and load then an other time. predesignating
> then before loading also doesn't work.

Specify an output argument for LOAD.

mydata = load('BartRPG.mat');

Then you're not attempting to add variables to the workspace, you're simply
adding fields to a variable that already exists. Since the error message
indicates that one of the variables in that MAT-file is named keuzem, with
this approach you'd need to refer to it as mydata.keuzem -- or if you wanted
to change the rest of your code as little as possible, put this line
immediately after your LOAD call:

keuzem = mydata.keuzem;

> i will post the m-file if its nessesary,
>
> hope someone can help me.
>
> bart,
> Student
> PS. please dont mind my english i'm living in the netherlands.

Your English was fine. Just one suggestion, though -- "i" is sqrt(-1). "I"
is the person who's speaking :)

--
Steve Lord
sl...@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ


bart Zaalberg

unread,
Feb 23, 2010, 2:40:22 PM2/23/10
to
thank you for the fast reply. got it to work :)

bart Zaalberg

unread,
Feb 23, 2010, 2:43:07 PM2/23/10
to

Jakob Thumm

unread,
Jun 4, 2017, 4:15:09 AM6/4/17
to
> ??? Error using ==> load
> Attempt to add "keuzem" to a static workspace.
> See MATLAB Programming, Restrictions on Assigning to Variables for
> details.
>
> Error in ==> RPGfinal at 7
> load('BartRPG.mat')
>
> i know i get a error because its i a static workspace but isn't there any way i can save my varaibles and load then an other time. predesignating then before loading also doesn't work.

-------------------------------------------

Hi,

I know, your problem is solved but for everybody, who has the same issue:

This error only occurs in nested functions. A classic case is the callback function of a button.
You can simply create a new function where you load the workspace normal. This is espacially useful if you have workspaces with a lot of variables.

An example:

function [obj] = myFrame()
.
.
.
myButton = uicontrol('Style','pushbutton',...
'String','push me','Position',[350,5,100,24],...
'Callback',{@callback_myButton});
.
.
.
function callback_myButton(source, eventdata)
myVar = loadMyWorkspace(); % this is a nested function
end
end

And the new function:

function [myVar] = loadMyWorkspace()
load('myWorkspace.mat'); % this is not.
end
0 new messages