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

조회 수: 3(최근 30일)
Srinivasa Raghavan Raghuraman
Srinivasa Raghavan Raghuraman 2021년 1월 21일
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
Srinivasa Raghavan Raghuraman
Srinivasa Raghavan Raghuraman 2021년 1월 22일
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
Srinivasa Raghavan Raghuraman
Srinivasa Raghavan Raghuraman 2021년 1월 22일
Thank you Bob Thompson and Stephen Cobeldick for your time and help. The indexing method works very well for my problem.

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by