How can I use a variable for formatting?
2 次查看(过去 30 天)
显示 更早的评论
Hi.
I have this line of code:
address = num2str(str2double(string(dec2bin(find(strcmp(dictionary,content(1:end-1)))))),'%04d');
It works, but I would like that the 4 is taken from a variable.
In particular, my variable is
numBits = ceil(log2(length(phrases)));
Is there a way to do so? Thanks
0 个评论
采纳的回答
Les Beckham
2022-4-14
编辑:Les Beckham
2022-4-14
You can also use %*d, like this
for k = 1:5
fprintf('%*d\n', k, 2*k); % the first argument (k) is the field width, 2*k is the value to print
end
Documentation is here
By the way, why are you converting to string and then to double and then back to a string?
5 个评论
Les Beckham
2022-4-14
编辑:Les Beckham
2022-4-14
If an answer in the forum solves your problem, it is considered gracious to accept it. If not please add a comment to request additional clarification. Thanks.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!