0
votes

I am trying to load multiple files from a directory I created, plot them in separate figures, and then output them as .tiff files.

I believe that I have all of the code needed for plotting the loaded files and then outputting them as .tiff files, but I am unable to get the files loaded into MATLAB initially to carry out the plotting and the output.

Function used:

function x = chaos(x0, lambda, vectorLength);
x0 = 0.5;
lambda = 3.8;
vectorLength = 1500;

x = zeros(vectorLength,1);
x(1) = x0;

for k=2:vectorLength,
    x(k) = lambda*x(k-1)*(1-x(k-1));
end

T = 2;
x1 = x(1:end-2*T);
x2 = x(T+1:end-T);
x3 = x(2*T+1:end);

figure('Color',[1 1 1]);
h = plot3(x1, x2, x3);
xlabel('x(t)');
ylabel('x(t+T)');
zlabel('x(t+2T)');

Code used for creating directory and file (working):

currentFolder = pwd;
mkdir('chaos');
for k = 1:30
    data=chaos(k);
    full_filename = fullfile(currentFolder,['\chaos\chaos' num2str(k) '.txt']);
    fid = fopen(full_filename,'w' );
    fprintf(fid,'%d\n',data);
    fclose(fid);
end
full_filename = fullfile(currentFolder,['\chaos\chaos1.txt']);
fileID = fopen(full_filename,'r');
formatSpec = '%f';
X = fscanf(fileID,formatSpec);
plot(X);

Code used for trying to load, plot, then output files from the created directory (not working):

for k = 1:30
    dir('chaos');
    x = load('chaos(k).txt');
    figure('Color', [1 1 1]);
    plot(x);
    pause(0.1);
    eval(sprintf('print -dtiff chaos%d', k));
end

I am expecting to have 30 figures plotted and outputted to the screen, and then have 30 figures outputted as .tiff files. The actual output is only showing the directory in the command window, and nothing is getting plotted or outputted as .tiff files.

EDIT: here is the updated code to fix the problem with the variable k: x = load(['chaos', num2str(k), '.txt']);

1
x = load('chaos(k).txt'); Here k is a part of the string, it is not a variable which you can iterate and please do not use eval - Sardar Usama

1 Answers

0
votes

Here's an issue:

for k = 1:30
    dir('chaos');
    x = load('chaos(k).txt');

Variable interpolation doesn't work like that in Matlab. If you want the value held in k to go into your string, you need to use string concatenation or sprintf:

for k = 1:30
    file = sprintf('chaos%d.txt', k);
    x = load(file);

Looks like you're doing the right thing further down in your output code; you just need to apply it here, too.

You probably should avoid eval, too. Call it like this:

print('-dtiff', sprintf('chaos%d', k));