Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
所以下面的代码可以用空格填充一个字符串,保持 max_len 的最大长度(包括空格和字符串)。
max_len = 20; spacing_arg = ['% ', max_len,'s']; padded_string = sprintf(spacing_arg, str);
现在如何使用正确的填充有效地做到这一点?
你是这个意思吗?
max_len = 20; spacing_arg = ['%-', num2str(max_len),'s']; padded_string = sprintf(spacing_arg, str);
max_len = 20; padded_str = repmat(' ',1, max_len); padded_str(1:min(20,length(str))) = str(1:min(20,length(str)));