can I conduce this command?
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
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 😚
0 Commenti
Risposte (1)
Shishir Reddy
il 14 Giu 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 Commento
Voss
il 28 Ago 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)
disp(x_1_new)
disp(x_2_old)
disp(x_2_new)
isequal(x_1_old,x_1_new)
isequal(x_2_old,x_2_new)
Vedere anche
Categorie
Scopri di più su Startup and Shutdown in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!