can I conduce this command?

1 次查看(过去 30 天)
omri dagan
omri dagan 2023-6-9
评论: Voss 2024-8-28
hello, I'm tring to save this string, but the format is the same and it seems weird to write the command like that...
x_1 = sprintf('%.*fcos(%.*ft+%.*f)+%.*fcos(%.*ft+%.*f)',presi,X(1),presi,w_n(1),presi,phi(1),presi,X(2),presi,w_n(2),presi,phi(2));
x_2 = sprintf('%.*fcos(%.*ft+%.*f)+%.*fcos(%.*ft+%.*f)',presi,X(1)*r(1),presi,w_n(1),presi,phi(1),presi,X(2)*r(2),presi,w_n(2),presi,phi(2));
there is a batter way to get this output?
The image below is markout of the output strings, beside the 'cos','t','(/)' the all is numbers thet I whold like that the user of the overall function will be able to determine with the variable pressi.
thank you
and sorry for my English 😚

回答(1 个)

Shishir Reddy
Shishir Reddy 2023-6-14
Hi dagan,
As per my understanding, you want to improve the syntax of the command and make it more readable.
sprintf can be useful for formatting strings with variable values, but it may seem cumbersome in this case. So, you can directly concatenate the string expressions using string concatenation (+ operator) in MATLAB as shown below.
x_1 = num2str(X(1)) + "cos(" + num2str(w_n(1)) + "t+" + num2str(phi(1)) + ") + " + num2str(X(2)) + "cos(" + num2str(w_n(2)) + "t+" + num2str(phi(2)) + ")";
x_2 = num2str(X(1)*r(1)) + "cos(" + num2str(w_n(1)) + "t+" + num2str(phi(1)) + ") + " + num2str(X(2)*r(2)) + "cos(" + num2str(w_n(2)) + "t+" + num2str(phi(2)) + ")";
This approach directly combines the values and strings using the + operator, eliminating the need for sprintf.
The num2str function is used to convert numeric values to strings.
Both approaches will produce the same result, but using string concatenation might be more readable and straightforward in this case.
For further reference, please refer these links to know more about ‘String concatenation’ and ‘num2str’
I hope this helps resolving the issue.
  1 个评论
Voss
Voss 2024-8-28
num2str is not necessary when using the string concatenation operator + because that operator already converts numbers to strings
X = rand(1,2);
w_n = rand(1,2);
phi = rand(1,2);
r = rand(1,2);
% with num2str
x_1_old = num2str(X(1)) + "cos(" + num2str(w_n(1)) + "t+" + num2str(phi(1)) + ") + " + num2str(X(2)) + "cos(" + num2str(w_n(2)) + "t+" + num2str(phi(2)) + ")";
x_2_old = num2str(X(1)*r(1)) + "cos(" + num2str(w_n(1)) + "t+" + num2str(phi(1)) + ") + " + num2str(X(2)*r(2)) + "cos(" + num2str(w_n(2)) + "t+" + num2str(phi(2)) + ")";
% without num2str
x_1_new = X(1) + "cos(" + w_n(1) + "t+" + phi(1) + ") + " + X(2) + "cos(" + w_n(2) + "t+" + phi(2) + ")";
x_2_new = X(1)*r(1) + "cos(" + w_n(1) + "t+" + phi(1) + ") + " + X(2)*r(2) + "cos(" + w_n(2) + "t+" + phi(2) + ")";
disp(x_1_old)
0.62894cos(0.73837t+0.22071) + 0.18368cos(0.52654t+0.31621)
disp(x_1_new)
0.62894cos(0.73837t+0.22071) + 0.18368cos(0.52654t+0.31621)
disp(x_2_old)
0.33926cos(0.73837t+0.22071) + 0.072774cos(0.52654t+0.31621)
disp(x_2_new)
0.33926cos(0.73837t+0.22071) + 0.072774cos(0.52654t+0.31621)
isequal(x_1_old,x_1_new)
ans = logical
1
isequal(x_2_old,x_2_new)
ans = logical
1

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by