Loading & Plotting Multiple Files From Directory

15 ビュー (過去 30 日間)
countryroad
countryroad 2019 年 4 月 17 日
コメント済み: Stephen23 2019 年 4 月 18 日
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
Walter Roberson 2019 年 4 月 18 日
dir() does not change working directory. cd() changes working directory.
Stephen23
Stephen23 2019 年 4 月 18 日
編集済み: Stephen23 2019 年 4 月 18 日
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
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 件のコメント
Geoff Hayes
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
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by