필터 지우기
필터 지우기

How to print symbolic representation of [4^1+1 4^2+1 4^3+1] rather than the actual numerical output?

조회 수: 3 (최근 30일)
I would like to add a line of texts like "points = 4^1+1 4^2+1 4^3+1" instead of "points = 5 17 65" to chart.
The current content of my code to print "points = 5 17 65" is as following:
for i = 1:3
n = 4^i + 1;
%{
do something with numeric values of n and i (i.e., i = 1, 2, 3)
%}
% Storing "number of points" in vector 'point'
point(i) = n;
end
disp(['Grid points = ' sprintf('%d ', point)])
Note that I do need the numerical values of n and i (i.e., i = 1, 2, 3 for n = 4^i + 1) to do some computations.
I found
syms i
may be appropriate for my intended use, but cannot figure out how to exactly apply it into my for-loop.
Please help me to figure out, any suggestions will be very appreciated!
Thanks!

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 5월 17일
편집: Ameer Hamza 2020년 5월 17일
point = 1:3;
disp(['Grid points = ' sprintf('4^%d ', point)])
Result
Grid points = 4^1 4^2 4^3
  댓글 수: 2
hmhuang
hmhuang 2020년 5월 18일
Thanks for your solution! Very intuitive!
But If I want add the text "Grid points = 4^1 4^2 4^3" to a chart by using:
for i = 1:3
n = 4^i + 1;
%{
do something with numeric values of n and i (i.e., i = 1, 2, 3)
%}
% Storing "number of points" in vector 'point'
point(i) = sprintf("4^%d", i);
end
txt = { "Resolution",
["Grid points = " point] };
text(xlim(1)*2, ylim(2)*0.01, txt, 'FontSize', 16);
An error happened due to the misuse of cell array:
Error using text
Cell array can contain only non-empty character vectors, string vectors, or numbers.
How can I resolve this problem?
Thank you
Ameer Hamza
Ameer Hamza 2020년 5월 18일
The syntax is not correct, according to MATLAB. But I didn't understand what you are trying to do here. Can you explain with an image what where you want to write the text()?

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by