fprintf cell array if its size changes

2 次查看(过去 30 天)
I want my cell Array to appear in matlab file. But the Array size may Change from time to time and I have to Change the number of "%s" in "fprintf". In the provided code I have 5 "%s" Statements. Later I might Need 7 or 10 etc. Is there an automatic way so that I dont have to adjust manually every time? The number of "%s" should depend on length of the cell Array.
myCellArray = {'t','x', 'k1',k2,'k3'};
fprintf(myFile, 'function dX = myFunction(%s,%s,%s,%s,%s)\n',myCellArray{:})
Result is like this, where the number of arguments may change:
function dX = myFunction(t,x,k1,k2,k3)

采纳的回答

dpb
dpb 2018-5-4
Sure...just build dynamic format string...
>> fmt=['function dX = myFunction(' repmat('%s,',1,length(myCellArray)-1) '%s)\n']
fmt =
'function dX = myFunction(%s,%s,%s,%s,%s)\n'
>> fprintf(fmt,myCellArray{:})
function dX = myFunction(t,x,k1,k2,k3)
>>

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by