Question

I am a beginner in Matlab programming and I have been trying to generate a *.dat file for my function. It sits in a loop that generates a number of subplots based on the number of parameters I initiate. It looks like this:

x=-10:10;
parameter_Array = [1 2 3 4 5];
for i = 1:length(parameter_Array)
subplot(length(parameter_Array),1,i);
S = x+parameter_Array[i];
plot(x,S);
end

What I would like to to is, for the first round out of five times in the above loops, write the arrays x and S into two columns separated by a tab, on the second round out of five, write S into a third column, on the third round, write S into a fourth column and so on. so the file should have the following form:

x S1 S2 S3 S4 S5

How do I go about doing this? I am trying to figure out a solution by inserting the following at the end of each loop:

fileID = fopen('MyFilet.dat','w');
fprintf(fileID,'%6s %12s\n','x','S(x)');
fprintf(fileID,'%6.6f %6.6f\n',x,S);
fclose(fileID);

The output file has two things wrong with it, 1. both columns have the same data, I believe its actually S not x; and 2. I only get the data from last iteration of the for loop(I am expecting this one)

I would be grateful for any help you can provide.

Thank you!

Was it helpful?

Solution

This will save as test.dat.

x=(-10:10)';
parameter_Array = [1 2 3 4 5]';
for i = 1:length(parameter_Array)
subplot(length(parameter_Array),1,i);
S(:,i) = x+parameter_Array(i);
plot(x,S(:,i));
end
D=[x S];
dlmwrite('test.dat',D,'\t')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top