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

23 views (last 30 days)
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 Comments
Stephen23
Stephen23 on 21 Jan 2021
"...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.

Sign in to comment.

Answers (1)

Bob Thompson
Bob Thompson on 21 Jan 2021
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 Comments
Stephen23
Stephen23 on 21 Jan 2021
Edited: Stephen23 on 21 Jan 2021
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.

Sign in to comment.

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!