开发者

Disp command format in MATLAB

开发者 https://www.devze.com 2023-03-22 16:26 出处:网络
I am the struggling with the following command.In reality, it is to be executed as a SQL statement.I am printing out the statement below using disp command.

I am the struggling with the following command. In reality, it is to be executed as a SQL statement. I am printing out the statement below using disp command.

Datevar = datestr(date,'mm/dd/yyyy') ; % 07/25/2011 

% Required command: execute SQLname @startdate = '7/25/2011'

% My current command:
disp([...
'execute SQLname ' ...
'@startdate = ' ''' Dat开发者_如何学运维evar ''' ...
])

I have tried many combinations but I am not able to hit this string right: @startdate = '7/25/2011'. Thanks!


Misplaced quote, at least in the example code you posted. That third line of the disp call is concatenating two separate strings, and the second one contains a literal "Datevar". You want this, which will concatenate the contents of the variable named Datevar.

sql = [...
'execute SQLname ' ...
'@startdate = ''' Datevar '''' ...
];
disp(sql);

IMHO, short queries like these are more readable if you construct them with sprintf, because you don't have to differentiate between internal quotes and delimiting quotes.

sql = sprintf('execute SQLname @startdate = ''%s''', Datevar);

If you include an example of the exact output you are getting, it's easier to diagnose problems like this.


It seems like you are do not want the leading zeroes provided by datestr. There are no date specifiers for returning non-padded day and month values. You can create the string you want by using DATEVEC and SPRINTF like this:

>> date = datevec('2011/07/25');
>> sprintf('execute SQLname @startdate = ''%u/%u/%u''', date([2 3 1]))

ans =

execute SQLname @startdate = '7/25/2011'
0

精彩评论

暂无评论...
验证码 换一张
取 消