How can I write this fprintf description to include multiple vectors?
7 views (last 30 days)
Show older comments
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 Comment
Rik
on 28 Jun 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).
Answers (1)
OCDER
on 28 Jun 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 Comments
See Also
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!