can I conduce this command?

1 visualización (últimos 30 días)
omri dagan
omri dagan el 9 de Jun. de 2023
Comentada: Voss el 28 de Ag. de 2024
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 😚

Respuestas (1)

Shishir Reddy
Shishir Reddy el 14 de Jun. de 2023
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 comentario
Voss
Voss el 28 de Ag. de 2024
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

Iniciar sesión para comentar.

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by