Printing repeating double header with dlmwrite and for loop

4 vues (au cours des 30 derniers jours)
DC
DC le 6 Déc 2016
Modifié(e) : Jan le 6 Déc 2016
I'm trying to print a '.dat' file with the code below that produces a repeating double header 776 times. The value at the end of each header line should change by +1.
for ii = 103:878
dlmname3 = 'Input_commands.dat';
header={'*Elset, elset=CALIBRATED_210_FINAL_VOLUME' num2str(ii)};
for ik = 1:776
header2={'*Include, input=Final_element_' num2str(ik) '.dat'};
end
dlmwrite(dlmname3, header,'delimiter','','-append');
dlmwrite(dlmname3, header2,'delimiter','','-append');
fclose all;
end
I would like this code to produce:
*Elset, elset=CALIBRATED_210_FINAL_VOLUME103
*Include, input=Final_element_1.dat
*Elset, elset=CALIBRATED_210_FINAL_VOLUME104
*Include, input=Final_element_2.dat
*Elset, elset=CALIBRATED_210_FINAL_VOLUME105
*Include, input=Final_element_3.dat
and so on up volume 878 and final_element_776...
However it is currently producing:
*Elset, elset=CALIBRATED_210_FINAL_VOLUME103
*Include, input=Final_element_776.dat
*Elset, elset=CALIBRATED_210_FINAL_VOLUME104
*Include, input=Final_element_776.dat
*Elset, elset=CALIBRATED_210_FINAL_VOLUME105
*Include, input=Final_element_776.dat
etc...
I've tried a few variations but it's never quite right. I think the problem is (ik) finishes before the second iteration of (ii) hence 776 showing up on all even lines. I know I could normally do it with one loop but I need the alternate lines to use and have different values.
I think I am missing something simple for the solution so apologies in advance for that but also a thank you in advance for any help you might be able to provide!

Réponse acceptée

Jan
Jan le 6 Déc 2016
Modifié(e) : Jan le 6 Déc 2016
dlmwrite is not useful here. Faster:
dlmname3 = 'Input_commands.dat';
fid = fopen(dlmname, 'W'); % Upper-case W
if fid == -1
error('Cannot open file: %s', dlmname3); % Never open without a check
end
for ii = 103:878
fprintf(fid, '*Elset, elset=CALIBRATED_210_FINAL_VOLUME%d\n', ii);
fprintf(fid, '*Include, input=Final_element_%d.dat\n', ii-102);
end
fclose(fid);

Plus de réponses (1)

DC
DC le 6 Déc 2016
I've since solved this having come back to it after a little break - think I'd been staring at the screen for too long. It was a VERY simple fix as suspected. Nothing at all ground breaking but will post it on the off chance it might help someone, somewhere, someday.
for ii = 103:878
dlmname3 = 'Input_commands.dat';
header={'*Elset, elset=CALIBRATED_210_FINAL_VOLUME' num2str(ii)};
header2={'*Include, input=Final_element_' num2str(ii-102) '.dat'};
dlmwrite(dlmname3, header,'delimiter','','-append');
dlmwrite(dlmname3, header2,'delimiter','','-append');
end

Catégories

En savoir plus sur Get Started with MuPAD dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by