How can I keep use of my existing code and append the output to 1 file n times?
Show older comments
Hello, I'm hoping someone could help me out as I've been stuck on this problem for a while. Currently I have code that edits an XML file and creates random values at the node specified. I am looking to find a way to use this code to make the same random changes but to each time i make a change, append that output to the bottom of 1 file. If I am not being clear, I will try to illustrate what I am talking about here:
I want my program to say something like "Enter n times to run:" and the user would input "100". The program would then make the changes provided in the code below 100 times and each time append the changes to the bottom of the previous iteration. So the end result would essentially be 100 times the file all in one file.
My code is provided here below. Thank you so much and I really hope someone can provide some insight.
xmlfile = fullfile(matlabroot, 'toolbox/matlab/general/ivan/test.xml');
xDoc = xmlread(xmlfile);
allListItems = xDoc.getElementsByTagName('Consignee_Name');
qtssao=allListItems.getLength;
thisListItem=allListItems.item(qtssao-1)
childNode = thisListItem.getFirstChild.getData;
thisListItem.getFirstChild.setData(num2str(RandomString1(64)));
allListItems = xDoc.getElementsByTagName('Total_Cargo_Weight');
qtssao=allListItems.getLength;
thisListItem=allListItems.item(qtssao-1)
childNode = thisListItem.getFirstChild.getData;
thisListItem.getFirstChild.setData(num2str(randi([1 100])));
%allListItems = xDoc.getElementsByTagName('');
%qtssao=allListItems.getLength;
%thisListItem=allListItems.item(qtssao-1)
%childNode = thisListItem.getFirstChild.getData;
%thisListItem.getFirstChild.setData('');
allListItems = xDoc.getElementsByTagName('Cargo_Control_Number');
qtssao=allListItems.getLength;
thisListItem=allListItems.item(qtssao-1)
childNode = thisListItem.getFirstChild.getData;
thisListItem.getFirstChild.setData(num2str(randi([100000000 300000000])));
allListItems = xDoc.getElementsByTagName('Unique_Consignment_Number');
qtssao=allListItems.getLength;
thisListItem=allListItems.item(qtssao-1)
childNode = thisListItem.getFirstChild.getData;
thisListItem.getFirstChild.setData(num2str(randi([100000 500000])));
%allListItems = xDoc.getElementsByTagName('');
%qtssao=allListItems.getLength;
%thisListItem=allListItems.item(qtssao-1)
%childNode = thisListItem.getFirstChild.getData;
%thisListItem.getFirstChild.setData('');
xmlwrite('test.xml',xDoc);
[EDITED, Jan, code formatted]
Answers (1)
Jan
on 30 Oct 2012
Perhaps you want:
n = input('Enter n tiomes to run: ');
for ii = 1:n
<your code here, or a call to the M-file your function is stored in>
end
% Probably the writing *after* the loop:
xmlwrite('test.xml',xDoc);
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!