Skip to content

Commit 4904155

Browse files
committed
jload load data to struct, test if loadbj input is buffer, update error msg
1 parent 55db193 commit 4904155

5 files changed

Lines changed: 26 additions & 21 deletions

File tree

jload.m

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
% jload
44
% or
55
% jload(fname)
6+
% varlist=jload(fname)
7+
% [varlist, header]=jload(fname)
68
% varlist=jload(fname,'param1',value1,'param2',value2,...)
79
%
810
% Load variables from a JSON or binary JSON file to a workspace
@@ -33,13 +35,16 @@
3335
% can be used to adjust the parsing options
3436
%
3537
% output:
36-
% varlist: a list of variables loaded
38+
% varlist: a struct with each subfield a variable stored in the file,
39+
% if output is ignored, the variables will be loaded to the
40+
% workspace specified by the 'ws' option, which by default
41+
% load the variables to the current workspace ('caller')
3742
%
3843
% examples:
3944
% jload % load all variables in jamdata.jamm to the 'caller' workspace
4045
% jload mydat.jamm
4146
% jload('mydat.jamm','vars', {'v1','v2',...}) % load selected variables
42-
% jload('mydat.jamm','simplifycell',1)
47+
% varlist=jload('mydat.jamm','simplifycell',1)
4348
%
4449
% license:
4550
% BSD or GPL version 3, see LICENSE_{BSD,GPLv3}.txt files for details
@@ -48,7 +53,7 @@
4853
%
4954

5055
if(nargin==0)
51-
filename='jamdata.jamm';
56+
filename=[pwd filesep 'jamdata.jamm'];
5257
end
5358

5459
opt=varargin2struct(varargin{:});
@@ -75,11 +80,6 @@
7580
header=loadfun(filename,'ObjectID',1, varargin{:});
7681
end
7782

78-
if(jsonopt('Header',0,opt))
79-
varargout{1}=header;
80-
return;
81-
end
82-
8383
allvar=fieldnames(header.WorkspaceHeader);
8484

8585
varlist=jsonopt('vars',allvar,opt);
@@ -96,10 +96,13 @@
9696
body=loadfun(filename,'ObjectID',2, varargin{:});
9797
end
9898

99-
for i=1:length(varlist)
100-
assignin(ws, varlist{i}, body.WorkspaceData.(varlist{i}));
101-
end
102-
103-
if(nargout>1)
104-
varargout{1}=varlist;
99+
if(nargout==0)
100+
for i=1:length(varlist)
101+
assignin(ws, varlist{i}, body.WorkspaceData.(varlist{i}));
102+
end
103+
else
104+
varargout{1}=rmfield(body.WorkspaceData,setdiff(fieldnames(body.WorkspaceData),varlist));
105+
if(nargout>1)
106+
varargout{2}=header;
107+
end
105108
end

jsave.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
%
4646

4747
if(nargin==0)
48-
filename='jamdata.jamm';
48+
filename=[pwd filesep 'jamdata.jamm'];
4949
end
5050

5151
opt=varargin2struct(varargin{:});

loadbj.m

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,10 @@
6363
fid = fopen(fname,'rb');
6464
string = fread(fid,inf,'uint8=>char')';
6565
fclose(fid);
66-
else
66+
elseif(regexp(fname, '^\s*[\[\{SCHiUIulmLMhdDTFZN]'))
6767
string=fname;
68+
else
69+
error_pos('input file does not exist or buffer is invalid');
6870
end
6971

7072
pos = 1; inputlen = length(string); inputstr = string;
@@ -156,7 +158,7 @@
156158
[cc,pos]=next_char(inputstr,pos);
157159
if(cc=='[')
158160
if(isfield(varargin{1},'noembedding_') && varargin{1}.noembedding_==1)
159-
error('ND array size specifier does not support embedding');
161+
error_pos('ND array size specifier does not support embedding');
160162
end
161163
varargin{1}.noembedding_=1;
162164
[dim, pos]=parse_array(inputstr, pos, varargin{:});
@@ -342,7 +344,7 @@
342344
end
343345
msg = [sprintf(msg, pos) ': ' ...
344346
inputstr(poShow(1):poShow(2)) '<error>' inputstr(poShow(3):poShow(4)) ];
345-
error( ['JSONLAB:InvalidFormat: ' msg] );
347+
error( ['JSONLAB:BJData:InvalidFormat: ' msg] );
346348
end
347349

348350
%%-------------------------------------------------------------------------

loadjson.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
end
101101
end
102102
else
103-
error('input file does not exist');
103+
error_pos('input file does not exist');
104104
end
105105

106106
pos = 1; inputlen = length(string); inputstr = string;
@@ -489,7 +489,7 @@
489489
end
490490
msg = [sprintf(msg, pos) ': ' ...
491491
inputstr(poShow(1):poShow(2)) '<error>' inputstr(poShow(3):poShow(4)) ];
492-
error( ['JSONLAB:InvalidFormat: ' msg] );
492+
error( ['JSONLAB:JSON:InvalidFormat: ' msg] );
493493
end
494494

495495
%%-------------------------------------------------------------------------

loadmsgpack.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@
188188
len = double(bytes2scalar(bytes(idx+1:idx+4), 'uint32'));
189189
[obj, idx] = parsemap(len, bytes, idx+5, varargin{:});
190190
otherwise
191-
error('transplant:parsemsgpack:unknowntype', ...
191+
error('JSONLAB:MSGPACK:InvalidFormat', ...
192192
['Unknown type "' dec2bin(currentbyte) '"']);
193193
end
194194
end

0 commit comments

Comments
 (0)