Multiple tables from multiple files
显示 更早的评论
I apologise if this...
- has been answered (I have been searching for a few hours now and can find things close but not quite working)
- falls into the 'use a single indexed array' response; I cannot picture this at present
I have a series of CSV files (66 at present but about to grow considerably).
The filenames are important (they tell me what I am looking at [xy, xVx, data] and the value of key parameters.
So, data19_1.csv, xy19_1.csv and xVx19_1.csv all deal with differing aspects of the same object. data3_1.csv, xy3_1.csv and xVx3_1.csv also refer to a single object but different to the first set of files.
I do not need to analyse the data sets collectively or as a whole; I simply need to generate graphs from each file.
I have the code for the graphing but I cannot for the life of me see how I can create a table for each file, using the filename as the table name.
My graphing code might give an idea of what I am looking to do...
figure(1)
hold on
axis equal
xlim ([-1.5 1.5])
ylim ([-1.5 1.5])
ax = gca;
ax.XAxisLocation = 'origin';
ax.YAxisLocation = 'origin';
scatter(xy19_1.x,xy19_1.y, '.');
plot(data19_1.x1, data19_1.y1,"O", "markerFaceColor", "r", "markerEdgeColor", "r", "markerSize", 8);
plot(data19_1.x2, data19_1.y2,"O", "markerFaceColor", "r", "markerEdgeColor", "r", "markerSize", 5);
figure(2)
hold on
ax = gca;
ax.XAxisLocation = 'bottom';
ax.YAxisLocation = 'right';
scatter(vx19_1.x,vx19_1.vx, '.');
10 个评论
Mario Malic
2020-8-20
First, obtain all .csv files in the folder, you get Filelist as a structure.
Filelist = dir('*.csv');
Then you can just take out their names with this command
File_Names = {Filelist.name}.'
Loop over all files
for ii = 1 : 1 : length(File_Names)
Table = readtable(File_Names(ii)) % Table will be overwritten on each step
% If you need it not be, than that is another issue
figure(ii) % I did not understand whether you need 3 or 66 graphs, adjust accordingly
% Include whatever goes for plotting
% Pay attention to order of the Filelist, you can sort it by some criteria if necessary,
%
% If you know that data1_1, data2_1 etc. are following 1, 4, 7,... order, you can
% make them plot using if conditions
end
I hope it helps.
Gary Newport
2020-8-25
"I actually need the table name to be the same as the file name,"
No, in fact you don't.
What you need is to keep the meta-data from the filename, but that does NOT mean that the variable has to have the same name as the file. In fact forcing meta-data into variable names and then trying to access them later is a guaranteed way to writing slow, complex, inefficient, fragile, buggy code that is hard to debug. Read this to know why:
Note that meta-data is data, and data should be stored in variables, not in variable names. Poorly designed data using lots of separate variables will slow down your code developement and lead to follow-up questions, like this one:
"I do not need to analyse the data sets collectively or as a whole; I simply need to generate graphs from each file."
Then you probably do not need to store them in one array either. Simply import and plot on each loop iteration.
Don't make this more complex than it needs to be!
Gary Newport
2020-8-25
"...you are suggesting I use the filename simply to identify the dataset, read the data into a single variable, plot it, then move to the next file?"
Yes, something like that would be simple and a better use of your time. You could use the filename to identify the dataset, or from vectors of known parameter values, whatever is easiest. Use sprintf as required, just as the documentation shows:
If you do not need all of the data to be available after the loop then something like this (pseudocode):
for ...
M = someImportingFunction(...)
plot(M)
end
Each loop uses exactly the same data variable, each loop plots the data. Adapt to suit your data.
Gary Newport
2020-8-25
"Is this in the correct direction?"
Yes. Obviously I cannot run it, but it looks very tidy. Nice work.
What happens when you run it?
Tip: many users prefer to use this third-party figure-saving function (download required):
Gary Newport
2020-8-25
Stephen23
2020-8-25
I don't have any experience with the XAxisLocation property, but the examples I could find showed it being used after plotting:
Gary Newport
2020-8-25
回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Text Data Preparation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!