MATLAB Answers

0

Loading & Plotting Multiple Files From Directory

Asked by countryroad on 17 Apr 2019
Latest activity Commented on by Stephen Cobeldick on 18 Apr 2019
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 files (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', num2str(k), '.txt']);
figure('Color', [1 1 1]);
plot(x);
pause(0.1);
eval(sprintf('print -dtiff chaos%d', k));
end

  2 Comments

dir() does not change working directory. cd() changes working directory.
Instead of this complex, obfuscated, liable-to-be-buggy command syntax warpped in eval:
eval(sprintf('print -dtiff chaos%d', k));
you should simply use function syntax:
print('-dtiff',...);
Also this rather defeats the purpose of fullfile:
fullfile(currentFolder,['\chaos\chaos' num2str(k) '.txt'])
Better something like this:
fullfile(currentFolder,'chaos',sprintf('chaos%d.txt',k))

Sign in to comment.

1 Answer

Answer by Geoff Hayes
on 18 Apr 2019
Edited by Geoff Hayes
on 18 Apr 2019

countryroad - what is the error message that you are observing? From your code that creates the files
full_filename = fullfile(currentFolder,['\chaos\chaos' num2str(k) '.txt']);
it looks like you create a folder called chaos and then write the files chaosX.txt to that folder. But I don't see where, in the code to read the files, you try to read the file from that folder. The
dir('chaos');
just lists the files in the chaos folder (see dir for details). Perhaps you mean change directory instead? Or why not just use fullfile as before
x = load(fullfile('chaos', ['chaos', num2str(k), '.txt']));

  3 Comments

The error message is: "Error using load
Unable to read file 'chaos1.txt'.
No such file or directory."
I am also getting an error message now for the line:
X = fscanf(fileID,formatSpec);
That says "Error using fscanf
Invalid file identifier. Use
fopen to generate a valid file
identifier."
If the error message is Unable to read file 'chaos1.txt'. then you probably just need to add the chaos folder...
I recommend that every time you call fopen you also use its second output like this:
[fid,msg] = fopen(...)
assert(fid>=3,msg)
and it will display about why that file could not be found or opened.

Sign in to comment.