MATLAB Answers

Why setAttribute fails when passing one string as the value in 2016b?

17 views (last 30 days)
Hadi Salimi
Hadi Salimi on 29 Sep 2020 at 7:14
Commented: Walter Roberson on 29 Sep 2020 at 7:47
I am trying to extract the information of the a simulink.busElement class and write them into one xml file like this:
newSigElem = docNode.createElement('SignalElement'); % creating a new xml element
parentElement.appendChild(newSigElem);
newSigElem.setAttribute('Name', busElem.Name); % works with all versions. (busElem is of type simulink.busElement)
newSigElem.setAttribute('Dimensions', string(busElem.Dimensions)); % fails on Matlab 2016b, but work fine with 2017b+
newSigElem.setAttribute('SampleTime', string(busElem.SampleTime)); % fails on Matlab 2016b, but work fine with 2017b+
But the above code fails to be executed on Matlab 2016b. It works fine with Matlab 2017b and later. Can someone tell me why the failure happening? How can I change the code to work on 2016b as well?
Thanks

  0 Comments

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 29 Sep 2020 at 7:22
string objects were introduced in r2016b, and it took a couple of releases for them to be accepted by all functions. Simulink in particular lagged behind: some parts of Simulink did not accept string objects until R2017b (and even later for some more obscure parts)

  2 Comments

Hadi Salimi
Hadi Salimi on 29 Sep 2020 at 7:29
Thanks Walter for the answer.
By trial and error I found that if I convert the strings into char arrays, it works fine on all Matlab versions:
newSigElem.setAttribute('Dimensions', char(string(busElem.Dimensions)));
Is it safe to do like this for All matlab versions (2016b and later)?
Walter Roberson
Walter Roberson on 29 Sep 2020 at 7:47
Yes, that should be fine.
However, there is essentially no control over how string() converts numeric values into string objects. For certainty you should consider using sprintf() or using num2str() passing in a format specification, or (r2016b and later) using compose() with a character vector for the format specification (if you use a string object for the format specification then compose() returns a string object.)

Sign in to comment.

Products


Release

R2016b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!