Index: /issm/trunk/src/m/classes/model.m
===================================================================
--- /issm/trunk/src/m/classes/model.m	(revision 8951)
+++ /issm/trunk/src/m/classes/model.m	(revision 8952)
@@ -405,79 +405,27 @@
 	 methods (Static)
 		 function md = loadobj(md) % {{{
-			 % This function is directly called by matlab when
-			 % a model object is loaded. If the input is a struct
-			 % it is an old version of model and old fields must be 
-			 % recovered (make sure they are in the deprecated
+			 % This function is directly called by matlab when a model object is
+			 % loaded. If the input is a struct it is an old version of model and
+			 % old fields must be recovered (make sure they are in the deprecated
 			 % model properties)
+
+			 if verLessThan('matlab','7.9'),
+				 disp('Warning: your matlab version is old and there is a risk that load does not work correctly');
+				 disp('         if the model is not loaded correctly, rename temporarily loadobj so that matlab does not use it');
+
+				 % This is a Matlab bug: all the fields of md have their default value
+				 % md.name = {''    [1]    'String'}
+				 % instead of recovering the saved fields
+				 % 
+				 % Example of error message:
+				 % Warning: Error loading an object of class 'model':
+				 % Undefined function or method 'exist' for input arguments of type 'cell'
+				 %
+				 % This has been fixed in MATLAB 7.9 (R2009b) and later versions
+			 end
 
 			 if isstruct(md)
 				 disp('Recovering model object from a previous version');
-
-				 %Ok the loaded model is a struct, initialize output and recover all fields
-				 structmd=md;
-				 md=model;
-
-				 structfields=fields(structmd);
-				 modelprops  =properties('model');
-
-				 for i=1:length(structfields),
-					 fieldname =structfields{i};
-					 fieldvalue=getfield(structmd,fieldname);
-					 if ismember(fieldname,modelprops),
-						 md=setfield(md,fieldname,fieldvalue);
-					 end
-				 end
-
-				 %Field name change
-				 if isfield(structmd,'drag'), md.drag_coefficient=structmd.drag; end
-				 if isfield(structmd,'p'), md.drag_p=structmd.p; end
-				 if isfield(structmd,'q'), md.drag_q=structmd.p; end
-				 if isfield(structmd,'B'), md.rheology_B=structmd.B; end
-				 if isfield(structmd,'n'), md.rheology_n=structmd.n; end
-				 if isfield(structmd,'melting'), md.basal_melting_rate=structmd.melting; end
-				 if isfield(structmd,'melting_rate'), md.basal_melting_rate=structmd.melting_rate; end
-				 if isfield(structmd,'accumulation'), md.surface_mass_balance=structmd.accumulation; end
-				 if isfield(structmd,'accumulation_rate'), md.surface_mass_balance=structmd.accumulation_rate; end
-				 if isfield(structmd,'numberofgrids'), md.numberofnodes=structmd.numberofgrids; end
-				 if isfield(structmd,'numberofgrids2d'), md.numberofnodes2d=structmd.numberofgrids2d; end
-				 if isfield(structmd,'gridonhutter'), md.nodeonhutter=structmd.gridonhutter; end
-				 if isfield(structmd,'gridonmacayeal'), md.nodeonmacayeal=structmd.gridonmacayeal; end
-				 if isfield(structmd,'gridonpattyn'), md.nodeonpattyn=structmd.gridonpattyn; end
-				 if isfield(structmd,'gridonstokes'), md.nodeonstokes=structmd.gridonstokes; end
-				 if isfield(structmd,'uppergrids'), md.uppernodes=structmd.uppergrids; end
-				 if isfield(structmd,'lowergrids'), md.lowernodes=structmd.lowergrids; end
-				 if isfield(structmd,'gridonbed'), md.nodeonbed=structmd.gridonbed; end
-				 if isfield(structmd,'gridonsurface'), md.nodeonsurface=structmd.gridonsurface; end
-				 if isfield(structmd,'extractedgrids'), md.extractednodes=structmd.extractedgrids; end
-				 if isfield(structmd,'gridoniceshelf'), md.nodeoniceshelf=structmd.gridoniceshelf; end
-				 if isfield(structmd,'gridonicesheet'), md.nodeonicesheet=structmd.gridonicesheet; end
-				 if isfield(structmd,'gridonwater'), md.nodeonwater=structmd.gridonwater; end
-				 if isfield(structmd,'gridonnuna'), md.nodeonnuna=structmd.gridonnuna; end
-				 if isfield(structmd,'gridonboundary'), md.nodeonboundary=structmd.gridonboundary; end
-				 if isfield(structmd,'spcvelocity'), 
-					 md.spcvx=NaN*ones(md.numberofnodes,1);
-					 md.spcvy=NaN*ones(md.numberofnodes,1);
-					 md.spcvz=NaN*ones(md.numberofnodes,1);
-					 pos=find(structmd.spcvelocity(:,1)); md.spcvx(pos)=structmd.spcvelocity(pos,4); 
-					 pos=find(structmd.spcvelocity(:,2)); md.spcvy(pos)=structmd.spcvelocity(pos,5); 
-					 pos=find(structmd.spcvelocity(:,3)); md.spcvz(pos)=structmd.spcvelocity(pos,6); 
-				 end
-
-				 %Field class change
-				 if (isfield(structmd,'type') & ischar(structmd.type)), 
-					 if strcmpi(structmd.type,'2d'), md.dim=2; end
-					 if strcmpi(structmd.type,'3d'), md.dim=3; end
-				 end
-				 if isnumeric(md.verbose), md.verbose=verbose; end
-
-				 %New fields
-				 if ~isfield(structmd,'upperelements');
-					 md.upperelements=transpose(1:md.numberofelements)+md.numberofelements2d;
-					 md.upperelements(end-md.numberofelements2d+1:end)=NaN;
-				 end
-				 if ~isfield(structmd,'lowerelements');
-					 md.lowerelements=transpose(1:md.numberofelements)-md.numberofelements2d;
-					 md.lowerelements(1:md.numberofelements2d)=NaN;
-				 end
+				 md = structtomodel(model,md);
 			 end
 		 end% }}}
@@ -614,5 +562,83 @@
 		 end
 		 %}}}
+		 function md = structtomodel(md,structmd) % {{{
+
+			 if ~isstruct(structmd) error('input model is not a structure'); end
+
+			 %loaded model is a struct, initialize output and recover all fields
+			 md = structtoobj(model,structmd);
+
+			 %Field name change
+			 if isfield(structmd,'drag'), md.drag_coefficient=structmd.drag; end
+			 if isfield(structmd,'p'), md.drag_p=structmd.p; end
+			 if isfield(structmd,'q'), md.drag_q=structmd.p; end
+			 if isfield(structmd,'B'), md.rheology_B=structmd.B; end
+			 if isfield(structmd,'n'), md.rheology_n=structmd.n; end
+			 if isfield(structmd,'melting'), md.basal_melting_rate=structmd.melting; end
+			 if isfield(structmd,'melting_rate'), md.basal_melting_rate=structmd.melting_rate; end
+			 if isfield(structmd,'accumulation'), md.surface_mass_balance=structmd.accumulation; end
+			 if isfield(structmd,'accumulation_rate'), md.surface_mass_balance=structmd.accumulation_rate; end
+			 if isfield(structmd,'numberofgrids'), md.numberofnodes=structmd.numberofgrids; end
+			 if isfield(structmd,'numberofgrids2d'), md.numberofnodes2d=structmd.numberofgrids2d; end
+			 if isfield(structmd,'gridonhutter'), md.nodeonhutter=structmd.gridonhutter; end
+			 if isfield(structmd,'gridonmacayeal'), md.nodeonmacayeal=structmd.gridonmacayeal; end
+			 if isfield(structmd,'gridonpattyn'), md.nodeonpattyn=structmd.gridonpattyn; end
+			 if isfield(structmd,'gridonstokes'), md.nodeonstokes=structmd.gridonstokes; end
+			 if isfield(structmd,'uppergrids'), md.uppernodes=structmd.uppergrids; end
+			 if isfield(structmd,'lowergrids'), md.lowernodes=structmd.lowergrids; end
+			 if isfield(structmd,'gridonbed'), md.nodeonbed=structmd.gridonbed; end
+			 if isfield(structmd,'gridonsurface'), md.nodeonsurface=structmd.gridonsurface; end
+			 if isfield(structmd,'extractedgrids'), md.extractednodes=structmd.extractedgrids; end
+			 if isfield(structmd,'gridoniceshelf'), md.nodeoniceshelf=structmd.gridoniceshelf; end
+			 if isfield(structmd,'gridonicesheet'), md.nodeonicesheet=structmd.gridonicesheet; end
+			 if isfield(structmd,'gridonwater'), md.nodeonwater=structmd.gridonwater; end
+			 if isfield(structmd,'gridonnuna'), md.nodeonnuna=structmd.gridonnuna; end
+			 if isfield(structmd,'gridonboundary'), md.nodeonboundary=structmd.gridonboundary; end
+			 if isfield(structmd,'TEST'), md.dummy=structmd.TEST; end
+
+			 %Field class change
+			 if (size(structmd.pressureload,2)==3 | size(structmd.pressureload,2)==5),
+				 md.pressureload=[structmd.pressureload...
+					 WaterEnum*structmd.elementoniceshelf(structmd.pressureload(:,end))+AirEnum*structmd.elementonicesheet(structmd.pressureload(:,end))];
+			 end
+			 if (isfield(structmd,'type') & ischar(structmd.type)), 
+				 if strcmpi(structmd.type,'2d'), md.dim=2; end
+				 if strcmpi(structmd.type,'3d'), md.dim=3; end
+			 end
+			 if isnumeric(md.verbose),
+				 md.verbose=verbose;
+			 end
+			 if isfield(structmd,'spcvelocity'), 
+				 md.spcvx=NaN*ones(md.numberofnodes,1);
+				 md.spcvy=NaN*ones(md.numberofnodes,1);
+				 md.spcvz=NaN*ones(md.numberofnodes,1);
+				 pos=find(structmd.spcvelocity(:,1)); md.spcvx(pos)=structmd.spcvelocity(pos,4); 
+				 pos=find(structmd.spcvelocity(:,2)); md.spcvy(pos)=structmd.spcvelocity(pos,5); 
+				 pos=find(structmd.spcvelocity(:,3)); md.spcvz(pos)=structmd.spcvelocity(pos,6); 
+			 end
+			 if isfield(structmd,'spcvelocity'), 
+				 md.spcvx=NaN*ones(md.numberofnodes,1);
+				 md.spcvy=NaN*ones(md.numberofnodes,1);
+				 md.spcvz=NaN*ones(md.numberofnodes,1);
+				 pos=find(structmd.spcvelocity(:,1)); md.spcvx(pos)=structmd.spcvelocity(pos,4); 
+				 pos=find(structmd.spcvelocity(:,2)); md.spcvy(pos)=structmd.spcvelocity(pos,5); 
+				 pos=find(structmd.spcvelocity(:,3)); md.spcvz(pos)=structmd.spcvelocity(pos,6); 
+			 end
+
+			 %New fields
+			 if ~isfield(structmd,'upperelements');
+				 md.upperelements=transpose(1:md.numberofelements)+md.numberofelements2d;
+				 md.upperelements(end-md.numberofelements2d+1:end)=NaN;
+			 end
+			 if ~isfield(structmd,'lowerelements');
+				 md.lowerelements=transpose(1:md.numberofelements)-md.numberofelements2d;
+				 md.lowerelements(1:md.numberofelements2d)=NaN;
+			 end
+			 if ~isfield(structmd,'diagnostic_ref');
+				 md.diagnostic_ref=NaN*ones(md.numberofnodes2d,6);
+			 end
+		 end% }}}
 		 function mdstruct=struct(md) % {{{1
+			 %Cast operator needed because of Dependent Hidden properties
 			 mdstruct=struct();
 			 modelprops=properties('model');
Index: sm/trunk/src/m/model/structtomodel.m
===================================================================
--- /issm/trunk/src/m/model/structtomodel.m	(revision 8951)
+++ 	(revision )
@@ -1,63 +1,0 @@
-function md=structtomodel(md,structmd)
-%STRUCTTOMODEL - convert a structure into a model
-%
-%   This function will take all the fields in structmd and copy them to corresponding
-%   fields in the md @model class instance. If the field does not exist in md, it is dropped.
-%
-%   Usage:
-%      md=structtomodel(md,structmd)
-
-structfields=fields(structmd);
-for i=1:length(structfields),
-	field=structfields(i);field=field{1};
-	fieldval=getfield(structmd,field);
-	if isfield(struct(md),field),
-		md=setfield(md,field,fieldval);
-	end
-end
-
-%some special treatment for models of previous ISSM versions
-
-%Field name change
-if isfield(structmd,'drag'), md.drag_coefficient=structmd.drag; end
-if isfield(structmd,'p'), md.drag_p=structmd.p; end
-if isfield(structmd,'q'), md.drag_q=structmd.p; end
-if isfield(structmd,'B'), md.rheology_B=structmd.B; end
-if isfield(structmd,'n'), md.rheology_n=structmd.n; end
-if isfield(structmd,'melting'), md.basal_melting_rate=structmd.melting; end
-if isfield(structmd,'melting_rate'), md.basal_melting_rate=structmd.melting_rate; end
-if isfield(structmd,'accumulation'), md.surface_mass_balance=structmd.accumulation; end
-if isfield(structmd,'accumulation_rate'), md.surface_mass_balance=structmd.accumulation_rate; end
-if isfield(structmd,'numberofgrids'), md.numberofnodes=structmd.numberofgrids; end
-if isfield(structmd,'numberofgrids2d'), md.numberofnodes2d=structmd.numberofgrids2d; end
-if isfield(structmd,'gridonhutter'), md.nodeonhutter=structmd.gridonhutter; end
-if isfield(structmd,'gridonmacayeal'), md.nodeonmacayeal=structmd.gridonmacayeal; end
-if isfield(structmd,'gridonpattyn'), md.nodeonpattyn=structmd.gridonpattyn; end
-if isfield(structmd,'gridonstokes'), md.nodeonstokes=structmd.gridonstokes; end
-if isfield(structmd,'uppergrids'), md.uppernodes=structmd.uppergrids; end
-if isfield(structmd,'lowergrids'), md.lowernodes=structmd.lowergrids; end
-if isfield(structmd,'gridonbed'), md.nodeonbed=structmd.gridonbed; end
-if isfield(structmd,'gridonsurface'), md.nodeonsurface=structmd.gridonsurface; end
-if isfield(structmd,'extractedgrids'), md.extractednodes=structmd.extractedgrids; end
-if isfield(structmd,'gridoniceshelf'), md.nodeoniceshelf=structmd.gridoniceshelf; end
-if isfield(structmd,'gridonicesheet'), md.nodeonicesheet=structmd.gridonicesheet; end
-if isfield(structmd,'gridonwater'), md.nodeonwater=structmd.gridonwater; end
-if isfield(structmd,'gridonnuna'), md.nodeonnuna=structmd.gridonnuna; end
-if isfield(structmd,'gridonboundary'), md.nodeonboundary=structmd.gridonboundary; end
-
-%Field class change
-if (isfield(structmd,'type') & ischar(structmd.type)), 
-	if strcmpi(structmd.type,'2d'), md.dim=2; end
-	if strcmpi(structmd.type,'3d'), md.dim=3; end
-end
-if isnumeric(md.verbose), md.verbose=verbose; end
-
-%New fields
-if ~isfield(structmd,'upperelements');
-	md.upperelements=transpose(1:md.numberofelements)+md.numberofelements2d;
-	md.upperelements(end-md.numberofelements2d+1:end)=NaN;
-end
-if ~isfield(structmd,'lowerelements');
-	md.lowerelements=transpose(1:md.numberofelements)-md.numberofelements2d;
-	md.lowerelements(1:md.numberofelements2d)=NaN;
-end
Index: /issm/trunk/src/m/utils/Miscellaneous/structtoobj.m
===================================================================
--- /issm/trunk/src/m/utils/Miscellaneous/structtoobj.m	(revision 8952)
+++ /issm/trunk/src/m/utils/Miscellaneous/structtoobj.m	(revision 8952)
@@ -0,0 +1,16 @@
+function obj=structtoobj(obj,S),
+%Convert struct to object
+
+	%Get object and structure fields
+	structfields=fields(S);
+	objprops    =properties(class(obj));
+
+	%recover object properties
+	for i=1:length(structfields),
+		fieldname =structfields{i};
+		if ismember(fieldname,objprops),
+			fieldvalue=getfield(S,fieldname);
+			obj=setfield(obj,fieldname,fieldvalue);
+		end
+	end
+end
Index: /issm/trunk/src/m/utils/Model/loadmodel.m
===================================================================
--- /issm/trunk/src/m/utils/Model/loadmodel.m	(revision 8951)
+++ /issm/trunk/src/m/utils/Model/loadmodel.m	(revision 8952)
@@ -21,19 +21,15 @@
 	warning off MATLAB:unknownElementsNowStruc;
 	struc=load(path,'-mat');
-
 	warning on MATLAB:unknownElementsNowStruc;
 
-	%Check that there is only one variable
 	name=char(fieldnames(struc));
 	if size(name,1)>1,
 		error(['loadmodel error message: file ' path ' contains several variables. Only one model should be present.']); 
 	end
-	md=eval(['struc.' name]);
+	md=struc.(name);
 	
 	%now, transform md into a new model
 	if ~strcmpi(class(md),'model') & ~strcmpi(class(md),'planet'),
-		md2=structtomodel(model,md);
-		md=md2;
-		clear md2;
+		md=structtomodel(model,md);
 	end
 	if nargout,
