Loading & Plotting Multiple Files From Directory
이전 댓글 표시
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
Walter Roberson
2019년 4월 18일
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))
답변 (1개)
Geoff Hayes
2019년 4월 18일
편집: Geoff Hayes
2019년 4월 18일
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
countryroad
2019년 4월 18일
Geoff Hayes
2019년 4월 18일
If the error message is Unable to read file 'chaos1.txt'. then you probably just need to add the chaos folder...
Stephen23
2019년 4월 18일
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.
카테고리
도움말 센터 및 File Exchange에서 File Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!