MATLAB Answers


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));
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);
Code used for creating directory and files (working):
currentFolder = pwd;
for k = 1:30
full_filename = fullfile(currentFolder,['\chaos\chaos' num2str(k) '.txt']);
fid = fopen(full_filename,'w' );
full_filename = fullfile(currentFolder,['\chaos\chaos1.txt']);
fileID = fopen(full_filename,'r');
formatSpec = '%f';
X = fscanf(fileID,formatSpec);
Code used for trying to load, plot, then output files from the created directory (not working):
for k = 1:30
x = load(['chaos', num2str(k), '.txt']);
figure('Color', [1 1 1]);
eval(sprintf('print -dtiff chaos%d', k));


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:
Also this rather defeats the purpose of fullfile:
fullfile(currentFolder,['\chaos\chaos' num2str(k) '.txt'])
Better something like this:

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
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']));


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
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(...)
and it will display about why that file could not be found or opened.

Sign in to comment.