How can I write this fprintf description to include multiple vectors?
8 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentario
Rik
el 28 de Jun. de 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).
Respuestas (1)
OCDER
el 28 de Jun. de 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});
0 comentarios
Ver también
Categorías
Más información sobre Data Type Conversion en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!