Help in Syntax of Eval and Sprintf

조회 수: 2 (최근 30일)
ARS
ARS 2013년 3월 14일
Hi,
I want to run the following expression:
*X = fts2mat(ESP('01-Jan-97::31-Dec-2012'))*
The above statemement works fine. ESP is my financial time seris(fts) container. I am getting the desired results in X.
But now I wish the dates to be passed from a cell array of date strings I have as mydate1{} and mydate2{} in place of the datestrings given in the first statement. I think it can be done via
*eval(sprintf(fts2mat(ESP('mydate1''::''mydate2'))));*
But the above gives error.
Help will be highly appreciated.
Regards,
AMD.

채택된 답변

ChristianW
ChristianW 2013년 3월 14일
Y = cell(size(mydate1)); % preallocate
for k = 1:length(mydate1)
str = [mydate1{k} '::' mydate2{k}]; % Date String Range
% str = sprintf('%s::%s',mydate1{k},mydate2{k}); % Alternative
Y{k} = fts2mat(EPS(str)); % output saved in cell Y
end
  댓글 수: 2
ARS
ARS 2013년 3월 14일
Thanks a lot.
Can you please explain this: str = sprintf('%s::%s',mydate1{k},mydate2{k});
I mean why can't it be
str = sprintf(mydate1{k},'%s::%s',mydate2{k});
Is there any order to follow? like.,,the operator :: needs to be in the start? but it is evaluated in the middle actually.
Regards
AMD.
ChristianW
ChristianW 2013년 3월 14일
doc sprintf
An example:
sprintf('answer=%d',3) % ans = 'answer=3'
sprintf('%d is the answer',3) % ans = '3 is the answer'
The Symbol %d determines where the variable (3) is placed in the string. Same with %s, s announces that the variable is a string.
sprintf('answer=%s','HELLO')
sprintf('%s is the answer','HELLO')

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

추가 답변 (0개)

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by