How to sequentially import csv files, access, edit and save the data?
15 次查看(过去 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
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:
回答(1 个)
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
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!