How can I write this fprintf description to include multiple vectors?

12 vues (au cours des 30 derniers jours)
Giovanni Virgen
Giovanni Virgen le 28 Juin 2018
Réponse apportée : OCDER le 28 Juin 2018
So I have to describe my answers by putting them into a single sentence. I have multiple vectors answers and one dot product. I can't get the vectors to display properly. For example, the sentence will say like "vectors a (insert vector) and b (insert vector) can be used to produce a+b which is (insert vector)...." I just need it to display the vectors properly within a sentence.
a = [10 7 -2]; b = [-4 6 -6]; z = a+b; k = a-b; x = dot(a,b); y = cross (a,b); c = 5*a;
  1 commentaire
Rik
Rik le 28 Juin 2018
fprintf doesn't really offer the possibility of giving a formatspec for a vector if you plan on including other elements as well. A common trick is to form the fprintf formatspec with sprintf and a few calls to size (don't forget to escape percent signs in sprintf with a second percent sign).

Connectez-vous pour commenter.

Réponses (1)

OCDER
OCDER le 28 Juin 2018
mat2str will help you write a matrix as a string.
a = [10 7 -2];
b = [-4 6 -6];
z = a+b;
k = a-b;
x = dot(a,b);
y = cross (a,b);
c = 5*a;
save('tempvar.mat', 'a', 'b', 'z', 'k', 'x', 'y', 'c');
T = load('tempvar.mat');
delete('tempvar.mat');
Var = [fieldnames(T)'; cellfun(@mat2str, struct2cell(T), 'un', 0)'];
fprintf('vectors %s (%s), ', Var{:, 1:end-1});
fprintf('vectors %s (%s).\n', Var{:, end});

Catégories

En savoir plus sur Data Type Conversion dans Help Center et File Exchange

Produits


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by