Changeset 28224


Ignore:
Timestamp:
04/10/24 04:32:28 (11 months ago)
Author:
Mathieu Morlighem
Message:

CHG: fixed modeltostruct, skipping subobjects

File:
1 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk-jpl/src/m/classes/model.m

    r28087 r28224  
    17531753                        random_part=fix(rand(1)*10000);
    17541754                        id=[md.miscellaneous.name '-' regexprep(datestr(now),'[^\w'']','') '-' num2str(random_part)  '-' getenv('USER') '-' oshostname() '.upload'];
    1755                         eval(['save ' id ' md']);
     1755                        save('id','md');
    17561756
    17571757                        %Now, upload the file:
     
    17651765
    17661766                        %get locally rid of file that was uploaded
    1767                         eval(['delete ' id]);
     1767                        delete(id);
    17681768
    17691769                end % }}}
     
    17821782
    17831783                        %get locally rid of file that was downloaded
    1784                         eval(['delete ' name]);
     1784                        delete(name);
    17851785
    17861786                end % }}}
    17871787                function saveasstruct(md,filename) % {{{
    17881788
    1789                         fields=sort(properties('model')); %sort fields so that comparison of binary files is easier
     1789                        %Get all model fields
     1790                        mdfields=properties('model');
     1791
    17901792                        disp('Converting all model fields to struct...');
    17911793                        warning off MATLAB:structOnObject
     1794                        for i=1:length(mdfields),
     1795
     1796                                %convert md field to struct
     1797                                field=mdfields{i};
     1798                                field_struct = struct(md.(field));
     1799
     1800                                %Check that there is no class remaining in the field
     1801                                subfields = fields(field_struct);
     1802                                for i=1:numel(subfields)
     1803                                        if isobject(field_struct.(subfields{i}))
     1804                                                disp(['skipping ' subfields{i} ' because it is an object'])
     1805                                                field_struct = rmfield(field_struct, subfields{i});
     1806                                        end
     1807                                end
     1808                                md.(field) = field_struct;
     1809                        end
     1810
     1811                        disp('Converting model to struct...');
     1812                        md=struct(md);
     1813
     1814                        disp(['Saving as ' filename '...']);
     1815                        warning on MATLAB:structOnObject
     1816                        save(filename,'md','-v7.3')
     1817
     1818                end % }}}
     1819                function savemodeljs(md,modelname,websiteroot,varargin) % {{{
     1820
     1821                        %the goal of this routine is to save the model as a javascript array that can be included in any html
     1822                        %file:
     1823
     1824                        options=pairoptions(varargin{:});
     1825                        optimization=getfieldvalue(options,'optimize',0);
     1826
     1827
     1828                        %disp:
     1829                        disp(['saving model ''' modelname ''' in file ' websiteroot '/js/' modelname '.js']);
     1830
     1831                        %open file for writing and declare the model:
     1832                        fid=fopen([websiteroot '/js/' modelname '.js'],'w');
     1833                        fprintf(fid,'var %s=new model();\n',modelname);
     1834
     1835                        %now go through all the classes and fwrite all the corresponding fields:
     1836
     1837                        fields=properties('model');
    17921838                        for i=1:length(fields),
    17931839                                field=fields{i};
    1794                                 md.(field) = struct(md.(field));
    1795                         end
    1796                         disp('Converting model to struct...');
    1797                         md=struct(md);
    1798                         warning on MATLAB:structOnObject
    1799                         disp(['Saving as ' filename '...']);
    1800                         save(filename,'md','-v7.3')
    1801                 end % }}}
    1802 function savemodeljs(md,modelname,websiteroot,varargin) % {{{
    1803 
    1804         %the goal of this routine is to save the model as a javascript array that can be included in any html
    1805         %file:
    1806 
    1807         options=pairoptions(varargin{:});
    1808         optimization=getfieldvalue(options,'optimize',0);
    1809 
    1810 
    1811         %disp:
    1812         disp(['saving model ''' modelname ''' in file ' websiteroot '/js/' modelname '.js']);
    1813 
    1814         %open file for writing and declare the model:
    1815         fid=fopen([websiteroot '/js/' modelname '.js'],'w');
    1816         fprintf(fid,'var %s=new model();\n',modelname);
    1817 
    1818         %now go through all the classes and fwrite all the corresponding fields:
    1819 
    1820         fields=properties('model');
    1821         for i=1:length(fields),
    1822                 field=fields{i};
    1823 
    1824                 %Some properties do not need to be saved
    1825                 if ismember(field,{'results','cluster' }),
    1826                         continue;
     1840
     1841                                %Some properties do not need to be saved
     1842                                if ismember(field,{'results','cluster' }),
     1843                                        continue;
     1844                                end
     1845
     1846                                %some optimization:
     1847                                if optimization==1,
     1848                                        %optimize for plotting only:
     1849                                        if ~ismember(field,{'geometry','mesh','mask'}),
     1850                                                continue;
     1851                                        end
     1852                                end
     1853
     1854                                %Check that current field is an object
     1855                                if ~isobject(md.(field))
     1856                                        error(['field ''' char(field) ''' is not an object']);
     1857                                end
     1858
     1859                                %savemodeljs for current object
     1860                                %disp(['javascript saving ' field '...']);
     1861                                savemodeljs(md.(field),fid,modelname);
     1862                        end
     1863
     1864                        %done, close file:
     1865                        fclose(fid);
    18271866                end
    1828 
    1829                 %some optimization:
    1830                 if optimization==1,
    1831                         %optimize for plotting only:
    1832                         if ~ismember(field,{'geometry','mesh','mask'}),
    1833                                 continue;
    1834                         end
    1835                 end
    1836 
    1837                 %Check that current field is an object
    1838                 if ~isobject(md.(field))
    1839                         error(['field ''' char(field) ''' is not an object']);
    1840                 end
    1841 
    1842                 %savemodeljs for current object
    1843                 %disp(['javascript saving ' field '...']);
    1844                 savemodeljs(md.(field),fid,modelname);
    1845         end
    1846 
    1847         %done, close file:
    1848         fclose(fid);
    1849 end
    18501867        end
    18511868end
Note: See TracChangeset for help on using the changeset viewer.