How to write to screen a set of variables with mixed types?

50 次查看(过去 30 天)
hello,
I am trying to write to screen a set of variables with mixed types. I have the code below
and I get the following display:
d=0.7874 mm , epsr=2.1 >>
a='d=0.7874 mm , epsr=2.1';
m=3;
k=7;
u=7.853535;
fprintf('%s %i \t %10.5f \n',[a m u])
d=0.7874 mm , epsr=2.1□□
help would be appreciated.
Thank you

回答(1 个)

Manikanta Aditya
Manikanta Aditya 2024-12-30,12:36
编辑:Manikanta Aditya 2024-12-30,12:40
It looks like you're trying to print a string along with integer and floating-point variables using fprintf. The issue arises because you're trying to concatenate different types into a single array, which fprintf doesn't handle well. Instead, you should pass each variable separately to fprintf.
Here's how you can modify your code to correctly display the variables:
a = 'd=0.7874 mm , epsr=2.1';
m = 3;
k = 7;
u = 7.853535;
% Use fprintf with separate arguments for each variable
fprintf('%s %i \t %10.5f \n', a, m, u);
d=0.7874 mm , epsr=2.1 3 7.85353
This will correctly print the string a, the integer m, and the floating-point number u in the desired format.
If you want to include the variable k as well, you can add it to the fprintf statement:
fprintf('%s %i \t %i \t %10.5f \n', a, m, k, u);
d=0.7874 mm , epsr=2.1 3 7 7.85353
This way, each variable is passed as a separate argument to fprintf, ensuring they are printed correctly.
Refer to the following doc:
I hope this helps.

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by