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!