Assign different values to variables in loop

I have the following code to generate a file: (result of output at the end)
prompt = 'Enter the file name (without extension) to be saved as Abaqus inputfile CSV: ';
fileName = input(prompt,'s');
fileName1=[fileName,'.csv'];
fileID = fopen(fileName1, 'w');
fID = fopen(fileName1, 'w');
snf = [ 1 2 3 4];
for snf = (1:size(snf,2))
fs4 = ('Force(name=''Load-%d'')\n');
mm4 = ('Moment(name=''Load-%d'')\n');
% fprintf(fID,fs1,nodenumber(sn),nodenumber(sn));
% fprintf(fID,fs2,nodenumber(sn),nodenumber(sn));
% fprintf(fID,fs3,nodenumber(sn),nodenumber(sn));
fprintf(fID,fs4,snf);
fprintf(fID,mm4,snf);
end
fclose(fID);
The resulting file is
Force(name='Load-1')
Moment(name='Load-1')
Force(name='Load-2')
Moment(name='Load-2')
Force(name='Load-3')
Moment(name='Load-3')
Force(name='Load-4')
Moment(name='Load-4')
What i have not been able to get is a file that has different load id for moment than force. What i want is as follows,
Force(name='Load-1')
Moment(name='Load-5')
Force(name='Load-2')
Moment(name='Load-6')
Force(name='Load-3')
Moment(name='Load-7')
Force(name='Load-4')
Moment(name='Load-8')
note: snf in the code is an array of serial numbers starting from 1 and i want to index the moments from where the force indexing ends.

4 件のコメント

Adam
Adam 2019 年 2 月 12 日
編集済み: Adam 2019 年 2 月 12 日
Surely just using something like:
snf2 = 5:8;
...
fprintf(fID,mm4,snf2);
would work. Preferably in a neater fashion though!
Yasir Zahoor
Yasir Zahoor 2019 年 2 月 12 日
Thanx adam for the response. Actually this is a fairly simplified example what i posted. In real case, snf could range from 1 to any number. So i want to access it in a generic way so that it starts from where snf ends, and goes on same number of times as snf does with an increment of 1.
Jan
Jan 2019 年 2 月 12 日
This sounds like the standard mistake of hiding indices in names of variables. You an array instead. See: TUTORIAL: How and why to avoid Eval
Yasir Zahoor
Yasir Zahoor 2019 年 2 月 12 日
Thankyou Jan for the info.

サインインしてコメントする。

 採用された回答

Yasir Zahoor
Yasir Zahoor 2019 年 2 月 12 日
編集済み: Yasir Zahoor 2019 年 2 月 12 日

0 投票

This worked for me
snm = snf(end)+1 : length(snf)*2; % Putting this before for loop
fprintf(fID,mm4,snm(snf)); % using this within for loop

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeEntering Commands についてさらに検索

製品

質問済み:

2019 年 2 月 12 日

編集済み:

2019 年 2 月 12 日

Community Treasure Hunt

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

Start Hunting!

Translated by