fileDatastore using for loop and scatter

1 次查看(过去 30 天)
akk
akk 2021-10-24
回答: Ive J 2021-10-24
Hi,
I am trying to load multiple .csv files from a folder (file1.csv, file2.csv, etc). Then I would like to plot specifc variables from each table onto the same plot. I can only get my code to plot one file though. I am not great with for loops...
fds = fileDatastore('*.csv', 'ReadFcn', @importdata)
fullFileNames = fds.Files
numFiles = length(fullFileNames)
% Loop over all files reading them in and plotting them.
for k = 1 : numFiles
fprintf('Now reading file %s\n', fullFileNames{k});
T = readtable('file4.csv');
scatter3( -T.Long, T.Lat, T.Depth_Meter_, 30, T.Temperature_Celsius_);hold on;
end

回答(1 个)

Ive J
Ive J 2021-10-24
You are not even reading your csv files; you just read the same file file4.csv over and over! Try this:
fds = fileDatastore('*.csv', 'ReadFcn', @readtable);
fullFileNames = fds.Files
numFiles = length(fullFileNames)
% Loop over all files reading them in and plotting them.
figure; hold on
for k = 1:numel(fds.Files)
fprintf('Now reading file %s\n', fds.Files{k});
T = read(fds);
scatter3( -T.Long, T.Lat, T.Depth_Meter_, 30, T.Temperature_Celsius_);
end
hold off

Community Treasure Hunt

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

Start Hunting!

Translated by