Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

savejson & Matlab 2017a: Undefined function or variable 'st'. #34

Closed
energynumbers opened this issue Mar 21, 2017 · 8 comments
Closed

Comments

@energynumbers
Copy link

Calls that formerly worked in Matlab 2016b and earlier, don't work in Matlab 2017a. From savejson I now get:

Undefined function or variable 'st'.
Error in savejson>matlabobject2json (line 448)
txt=struct2json(name,st,level,varargin{:});
@fangq fangq closed this as completed in 86ef12a Mar 27, 2017
@fangq
Copy link
Member

fangq commented Mar 27, 2017

@energynumbers, please check out my latest commit and let me know if that fixes the issue in the latest matlab.

@energynumbers
Copy link
Author

Thanks, it now runs without error. However, it produces JSON which is inconsistent with what I had before - it's losing some data. I'll try to create a small example that reproduces the problem. More news shortly.

@energynumbers
Copy link
Author

Looks to be some sort of confusion between a char array and a string array.

@energynumbers
Copy link
Author

energynumbers commented Mar 28, 2017

OK, the difference in behaviour between 2016b and 2017a is happening in loadjson.

Take this json file:

{
	"data": [
		[
			"a"
		],
		[
			"a\a"
		]
    ]
}

In 2016b, the resulting variable, after tst = loadjson(filename), has char arrays for tst.data{1}{1} and tst.data{2}{1}. And that happens whether or not there's a backslash in the second data item, in the json file.
In 2017a, the resulting variable has a string array for tst.data{1}{1}, and a char array for tst.data{2}{1}. If I remove the backslash from the second data item in the json file, then after loadjson, we're back to a char array for tst.data{1}{1}. That is, when I change the content of the second item, it changes the type of variable assigned to the first item. And this unpredictable behaviour between char arrays and strings, means that when I read in the json file, manipulate some of the elements, and then try to write it out, I'm trying to write out a cell array which contains a mix of strings and char arrays, and that doesn't go well - the whole cell array is written out as an array of blank elements.

@energynumbers
Copy link
Author

Hmmm, maybe savejson is just writing out blanks for strings, more generally, too?

@energynumbers
Copy link
Author

Ah, confirmed - savejson writes out blanks for strings:

a=struct()
a.b={string('hi')}
savejson('',a)

@energynumbers
Copy link
Author

energynumbers commented Mar 29, 2017

Going through savejson and changing the ischar tests to be ischar || isstring (likewise ~ischar && ~isstring) seems to fix this. I guess this probably applies to saveubjson too.

@Hell3000
Copy link

I am having the same problems.
Added the elseif with the matlab version check in savejson.m:
This might help:

function txt=obj2json(name,item,level,varargin)

if(iscell(item))
    txt=cell2json(name,item,level,varargin{:});
elseif(isstruct(item))
    txt=struct2json(name,item,level,varargin{:});
elseif(ischar(item))
    txt=str2json(name,item,level,varargin{:});
elseif ~verLessThan('matlab','9.1') && isstring(item)
    % versions 2016a and lower AND item is a string object 
    % (new string class since 2016b (9.1): isobject("String") returns true)
    
    % Don't know if this is correct in every case:
    % convert string to char by: item{:}
    txt=str2json(name,item{:},level,varargin{:});
elseif(isobject(item)) 
    txt=matlabobject2json(name,item,level,varargin{:});
else
    txt=mat2json(name,item,level,varargin{:});
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants