Import multiple files and plot data.

9 ビュー (過去 30 日間)
Rafael Brognoli Recco
Rafael Brognoli Recco 2020 年 7 月 16 日
コメント済み: Arthur Roué 2020 年 7 月 21 日
Hi,
  1. I am trying to write a script capable of opening all files from within the same folder.
  2. Each file has two columns coordinated (X, Y) in .ASC-II format.
  3. After importing all the files in the same folder, I need to plot all the graphics separately.
  4. The script I wrote is opening the correct number of files and also naming them correctly, however the 47 cells of myData have the values of the first file, as if it was copying 47 times the file “run_01”.
Could anyone suggest how to overcome this problem?
Thanks
-----
% Import data
run = importfile("D:\Desktop\Teste\run_01", [1, inf]); % describe the direction of the files.
numFiles = 47; % number of files you want to open (they must be inside the same folder)
startRow = 2; % The loop starts from the second file
endRow = inf;
myData = cell(numFiles,1);
for fileNum = 1:numFiles
fileName = sprintf('run_%02d',fileNum);
myData{fileNum} = importfile("D:\Desktop\Teste\run_01",[startRow,endRow]);
end
% To remove NaN cells
for fileNum = 1:numFiles
fileName = sprintf('run_%02d',fileNum);
myData{fileNum, 1} ([1:2,1003:end],:) = [];
end
% plot
figure
for i = 1:length(myData)
plot(myData{i,1}) ;
end

採用された回答

Arthur Roué
Arthur Roué 2020 年 7 月 16 日
編集済み: Arthur Roué 2020 年 7 月 16 日
Here is the mistake
for fileNum = 1:numFiles
fileName = sprintf('run_%02d',fileNum);
myData{fileNum} = importfile("D:\Desktop\Teste\run_01",[startRow,endRow]);
end
Should be
for fileNum = 1:numFiles
myData{fileNum} = importfile(sprintf('D:\Desktop\Teste\run_%02d',fileNum),[startRow,endRow]);
end
  2 件のコメント
Rafael Brognoli Recco
Rafael Brognoli Recco 2020 年 7 月 21 日
Thanks for the help, but your suggestion didn't work.
I found this recommendation to solve my problem.
for fileNum = 1:numFiles
fileName = sprintf('run_%02d',fileNum);
myData{fileNum} = importfile(fileName,[startRow,endRow]);
end
Arthur Roué
Arthur Roué 2020 年 7 月 21 日
No problem. It's basicaly the same solution, but I used your absolute path. The mistake in the original code was that you import always the same file (run_01) with this line
myData{fileNum} = importfile("D:\Desktop\Teste\run_01",[startRow,endRow]);
If my suggestion didn't work, it probably because the absolute path is not the same. The new line works (I think) because file to import is on MATLAB path.
If your problem is solved you can close and/or accept the answer.
Have nice day !

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeText Files についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by