Loading & Plotting Multiple Files From Directory
13 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Walter Roberson
le 18 Avr 2019
dir() does not change working directory. cd() changes working directory.
Stephen23
le 18 Avr 2019
Modifié(e) : Stephen23
le 18 Avr 2019
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))
Réponses (1)
Geoff Hayes
le 18 Avr 2019
Modifié(e) : Geoff Hayes
le 18 Avr 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 commentaires
Geoff Hayes
le 18 Avr 2019
If the error message is Unable to read file 'chaos1.txt'. then you probably just need to add the chaos folder...
Stephen23
le 18 Avr 2019
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.
Voir également
Catégories
En savoir plus sur File Operations dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!