How to sequentially import csv files, access, edit and save the data?

5 次查看(过去 30 天)
Hi friends,
I need to import .csv Data sequentially and read them as arrays, edit them and save them as seperate variables. I have tried to do the importing with "dir" with help of a for loop. But I do not know how to save the edited variables sequentially and because of this the for loop continuously replaces the previous variable with the data of the new one. I tried to plot the data out and I can get the plot figures of each instance of my loop, but the data is that of the last iteration.
%% Read The processes 1 to l
% Specify the folder
myFolder1 = 'C:\Users\';
% Get a list of all files in the folder with the desired file name pattern.
filePattern1 = fullfile(myFolder1, '*.csv'); % Format/ pattern
theFiles1 = dir(filePattern1);
for l = 1 :1: length(theFiles1)
baseFileName1 = theFiles1(l).name;
fullFileName1 = fullfile(theFiles1(l).folder, baseFileName1);
fprintf(1, 'Now reading %s\n', fullFileName1);
data_raw_P = readtable(fullFileName1);
%% % Now the csv is read as a table and saved in the variable data_raw_P. This causes the loop to replace the data read from the previous instance with the data from the latest instance. Is there any way to save as data_raw_P1, data_raw_P2, data_raw_P3 and so on? I need to use these data in the calculation and plotting part too.
%
% %% calculations
% %% figure,
figure,
%%%Since the graphs are not going to be replace with every iteration, my script generates the graphs for every step but I don't have any workspace variables to show for it!!!
end
  3 个评论
Stephen23
Stephen23 2021-1-21
"...I don't have any workspace variables to show for it"
Use indexing to store them in a variable, exactly like the MATLAB documentation shows:
Srinivasa Raghavan Raghuraman
Thank you Mathieu NOE and Stephen Cobeldick. I can now keep each of my iteration data in a variable with indexing.

请先登录,再进行评论。

回答(1 个)

Bob Thompson
Bob Thompson 2021-1-21
To save the results of a command each time you go through the loop you need to index the output. I recommend just adding a structure field to your existing files structure for the data.
%% Read The processes 1 to l
% Specify the folder
myFolder1 = 'C:\Users\';
% Get a list of all files in the folder with the desired file name pattern.
filePattern1 = fullfile(myFolder1, '*.csv'); % Format/ pattern
theFiles1 = dir(filePattern1);
for l = 1 :1: length(theFiles1)
baseFileName1 = theFiles1(i).name;
fullFileName1 = fullfile(theFiles1(i).folder, baseFileName1);
fprintf(1, 'Now reading %s\n', fullFileName1);
[theFiles1(i).Data] = readtable(fullFileName1);
I haven't tested that bit of code, as I dont' have your files. There might be some issues with the exact syntax, it's been a while since I've last made a structure, but it should get you in the right direction.
  2 个评论
Stephen23
Stephen23 2021-1-21
编辑:Stephen23 2021-1-21
Using the same structure returned by dir is a simple and efficient solution. A few remarks:
  • The square brackets are not required if allocating one array:
theFiles1(i).Data = ...
  • The end is missing at the end of the for loop.
  • The for loop uses l as the iterator, but inside the loop the code uses i as an index. This will cause an indexing error as i is otherwise undefined and so refers to the imaginary constant.
Srinivasa Raghavan Raghuraman
Thank you Bob Thompson and Stephen Cobeldick for your time and help. The indexing method works very well for my problem.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by