Multiple tables from multiple files

I apologise if this...
  1. has been answered (I have been searching for a few hours now and can find things close but not quite working)
  2. 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

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
Gary Newport 2020년 8월 25일
Thanks for this. I actually need the table name to be the same as the file name, but have found a work-around to the problem (I no longer need 100 files - hopefully). :)
Stephen23
Stephen23 2020년 8월 25일
편집: Stephen23 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
Gary Newport 2020년 8월 25일
Ah, we are holding a discussion across two questions; might we, therefore, use this one as the combination of the two.
If I am correct, 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?
If so, I simply need to read the data in, plot, export as a named graph (using the filename details to form the plot filename) and move to the next file.
I, thus, read in the filenames (as you suggest above), into a single table, where the code is standard, and then output from there, in a loop.
Am I correct there?
Stephen23
Stephen23 2020년 8월 25일
편집: Stephen23 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.
Sorry to abuse your time but using your code from the other response and adapting mine I came up with this...
D = '/Users/garynewport/Desktop/Programming/Restricted 3 Body Problem/Restricted 3 Body Problem';
C = {'094','114','124','134','096','106','116','126','098','108','118','128','138'};
xyG = figure('Name','xy - 0.9 * u^-8');
vxG = figure('Name','x-Vx - 0.9 * u^-8');
for k = 1:numel(C)
dtF = sprintf('dt%s.csv',C{k});
xyF = sprintf('xy%s.csv',C{k});
vxF = sprintf('vx%s.csv',C{k});
dt = readtable(fullfile(D,dtF));
xy = readtable(fullfile(D,xyF));
vx = readtable(fullfile(D,vxF));
hold on
axis equal
xyG.XAxisLocation = 'origin';
xyG.YAxisLocation = 'origin';
scatter(xy.x,xy.y, '.');
plot(dt.x1, dt.y1,"O", "markerFaceColor", "r", "markerEdgeColor", "r", "markerSize", 8);
plot(dt.x2, dt.y2,"O", "markerFaceColor", "r", "markerEdgeColor", "r", "markerSize", 5);
title('Particle path over time')
xlabel('x')
ylabel('y')
hold on
vxG.XAxisLocation = 'bottom';
vxG.YAxisLocation = 'right';
scatter(vx.x,vx.vx, '.');
title('Velocity of particle in the x direction when y = 0 and x < 0')
xlabel('x')
ylabel('Vx')
end
Is this in the correct direction? And what (laughable) errors have I made?
Stephen23
Stephen23 2020년 8월 25일
편집: Stephen23 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
Gary Newport 2020년 8월 25일
Hahaha, I hadn't tried; I was sure I had very basic concepts wrong.
I just tried it and got this error...
Unrecognized property 'XAxisLocation' for class 'matlab.ui.Figure'.
Error in graphingprocess (line 16)
xyG.XAxisLocation = 'origin';
I'll have look at the function, thank you.
Stephen23
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
Gary Newport 2020년 8월 25일
If you do not have an object reference then it needs to come before the plotting; with an object reference it must come after.
Who'd have thought it, eh?! :)

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

답변 (0개)

카테고리

도움말 센터File Exchange에서 Text Data Preparation에 대해 자세히 알아보기

제품

릴리스

R2020a

질문:

2020년 8월 19일

댓글:

2020년 8월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by