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

조회 수: 21 (최근 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
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
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
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.

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

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by