필터 지우기
필터 지우기

fprintf cell array if its size changes

조회 수: 1 (최근 30일)
Akbar
Akbar 2018년 5월 4일
답변: dpb 2018년 5월 4일
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