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

조회 수: 5 (최근 30일)
Giovanni Virgen
Giovanni Virgen 2018년 6월 28일
답변: OCDER 2018년 6월 28일
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
Rik
Rik 2018년 6월 28일
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).

댓글을 달려면 로그인하십시오.

답변 (1개)

OCDER
OCDER 2018년 6월 28일
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});

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by