필터 지우기
필터 지우기

How to use a display a string in fprintf

조회 수: 15 (최근 30일)
t sizzle
t sizzle 2021년 3월 19일
댓글: Gregg Cole 2022년 1월 14일
I am trying to write a section that will display two numerical values, with their units followed after each. The units will change depending upon what the user enters as their units. So far what I have tried are these two things. I can get it to display the numerical value but if I try to include the units it adds "random" numbers.
fprintf('\n The numerical value is %g %g, and the second value is %g %g.\n', numerical_value_1,num2str(units), numerical_value_two,num2str(units))
fprintf('\n The numerical value is %g %g, and the second value is %g %g.\n', numerical_value_1, units, numerical_value_two, units)

채택된 답변

Image Analyst
Image Analyst 2021년 3월 19일
Try
units = 'mm'; % Whatever it is, assuming it's a string.
fprintf('\n The numerical value is %g %s, and the second value is %g %s.\n', numerical_value_1, units, numerical_value_two, units)
fprintf('\n The numerical value is %g %s, and the second value is %g %s.\n', numerical_value_1, units, numerical_value_two, units)
  댓글 수: 2
t sizzle
t sizzle 2021년 3월 19일
This works perfectly thank you
Gregg Cole
Gregg Cole 2022년 1월 14일
Worked well as well, thank you

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Isabelle Foster
Isabelle Foster 2021년 3월 19일
If I understand you correctly, it will help you.
Example:
a = {'cats', 'dogs', 'birds'};
as = sort(a);
fprintf('%s, ', as{1:end-1});
fprintf('and %s are animals.\n', as{end});
  댓글 수: 3
Oliveira Pereira
Oliveira Pereira 2021년 9월 10일
thx
Gregg Cole
Gregg Cole 2022년 1월 14일
Great, thanks!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by