필터 지우기
필터 지우기

Using num2str to get brackets

조회 수: 4 (최근 30일)
Amandeep
Amandeep 2011년 9월 11일
Hi,
from
A = [ 54,0.04; 60,0.03; 65,0.02; 67,0.01];
I want to get:
B = [(54,0.04);(60,0.03);(65,0.02),(67,0.01)];
Which I will use to display in a plot. The problem is I can get the comma in the middle using num2str but not the brackets on either side.
I have tried:
for j = 1:size(A,1)
B(j) = ['[',num2str(A(:,1),'%5.3g),']'];
end
But I get a dimension error. Not sure what to try next.
Any help will be great. Thanks

채택된 답변

Oleg Komarov
Oleg Komarov 2011년 9월 11일
Another alternative using sprintf and mat2cell:
mat2cell(sprintf('(%2d,%3.2f)',A.'),1, repmat(9,1,numel(A)/2)).'
  댓글 수: 1
Andrei Bobrov
Andrei Bobrov 2011년 9월 11일
hi Oleg! more variant
reshape(sprintf('(%2d,%3.2f)',A.'),[],size(A,1))'

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

추가 답변 (1개)

Amandeep
Amandeep 2011년 9월 11일
I think I just figured it out, by usingthe code beolw in the plot:-
text(A(j,1),A(j,2),['(' num2str(A(j,1)),',' num2str(A(j,2)) ')']

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by