use*_*357 1 format matlab text-formatting
所以我编写代码来显示矩阵的某一行,具体取决于用户输入.但是,我需要以下列格式显示它:
Al (--%):
Cu (--%):
Mg (--%):
Mn (--%):
Si (--%):
Zn (--%):
Run Code Online (Sandbox Code Playgroud)
我需要找到一种方法来在命令窗口中以该格式显示输出行中的数字.这是我的代码:
%alloy compositions
a=[4.4 1.5 0.6 0 0; 0 1 0 0.6 0; 0 1.4 0 0 4.5; 1.6 2.5 0 0 5.6;
0 0.3 0 7 0];
%alloy compositions
A2042=a(1, :);
A6061=a(2, :);
A7005=a(3, :);
A7075=a(4, :);
prompt='Please enter an alloy code: ';
percents=input(prompt)
Run Code Online (Sandbox Code Playgroud)
哪个确实输出了我想要的行,我只需要将其符合给定的格式.所以,如果我得到输出行[4.4 1.5 0.6 0 0],我需要输出
Al (4.4%):
Cu (1.5%):
Mg (0.6%):
Mn (0%):
Si (0%):
Run Code Online (Sandbox Code Playgroud)
有谁知道如何做到这一点?我提前感谢你们.
metals = {'Al'; 'Cu';'Mg';'Mn';'Si'}; % metal names
percentages = [4.4;1.5;0.6;0;0]; % Corresponding percentages
Formatspec = ('%s (%1.1f%%):'); % Format specifier for your string
for ii = 1:numel(percentages)
str = sprintf(Formatspec,metals{ii},percentages(ii)); % Create a string
disp(str) % Display the string
end
Al (4.4%):
Cu (1.5%):
Mg (0.6%):
Mn (0.0%):
Si (0.0%):
Run Code Online (Sandbox Code Playgroud)
循环有点棘手和丑陋,但我sprintf抱怨没有被定义为cell-type输入.
这里有趣的一行是('%s (%1.1f%%):'),它指定你的格式首先包含一个字符串(%s),然后是一个空格和一个括号开头,随后是一个浮点数,前面有一个数字,一个数字前面有一个数字.最后用你的右括号和冒号关闭.