How to use object array elements with sprintf?
Show older comments
Hi, I'm having trouble getting sprintf to allow a string from an object property as an acceptable input.
I have a defined class that has a property 'name'. In another function I've used a loop to define 3 objects of that class into an object array called 'objArray'. I'd like to use sprintf to write a text command but I'm getting an error. I eventually want to write multiple commands with a for loop iterating over the 3 objects in objArray, but I can't figure out the syntax yet for event just the first element of the objArray...
Code:
cmd = sprintf('Object_name: %s',objArray(1).name)
Error:
Error using sprintf Function is not defined for 'cell' inputs.
I've also tried with {}...
Code:
cmd = sprintf('Object_name: %s',objArray(1).name)
Error:
Cell contents reference from a non-cell array object
Am I missing something about the sprintf syntax or should I be using a different function?
Thanks
Accepted Answer
More Answers (1)
Walter Roberson
on 18 Jun 2015
t = objArray(1).name;
cmd = sprintf('Object_name: %s', t{1});
Categories
Find more on Cell Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!