Defining table reference using list contents

2 views (last 30 days)
Please note that the following code examples are not meant to be correct but to illustrate the issue.
The following 2 lines ARE in my working MatLab code...
figure('Name','xy098')
scatter(xy098.x,xy098.y, '.');
plot(dt098.x1, dt098.y1,"O");
However, I have a lot of individual tables I would like to plot.
I envisage a list...
list1 = {'094','114','124','134','096','106','116','126','126','098','108','118','128','138'};
That I could then use to reference each table through looping through the list...
for i=0 to (length of list - 1) {
figure('Name','xy' + list1(i))
scatter(xy + list1(i).x,xy + list1(i).y, '.');
plot(dt + list1(i).x1, dt + list1(i).y1,"O", "markerFaceColor", "r", "markerEdgeColor", "r", "markerSize", 8);
}
My intention would then to output each graph produced as a .png for analysis (that's a later problem).
I cannot identify how to do such an assignment to variable name.
  2 Comments
Stephen23
Stephen23 on 25 Aug 2020
Edited: Stephen23 on 25 Aug 2020
"However, I have a lot of individual tables I would like to plot."
You did not tell us the most important information: how did all of these tables get into the workspace? The point at which they were created/imported is the correct place to fix this badly-designed data. For example, by load-ing into an output structure:
S = load(...);
and then trivially looping over its fields:
Your current inadvisable approach is one way that beginners force themselves into writing slow, complex, inefficient, obfuscated, buggy code that is hard to debug. Read this to know why:
For reference, an earlier question which hints at the start of this bad data design:
Gary Newport
Gary Newport on 25 Aug 2020
Hi Stephen
I had noted previous conversations on this very matter and would love to spend the time getting my head around how I might better undertake this task using a single data point. However, the data files are not analysed together. To be honest I just need to use MatLab to generate the graphs for me and output them in .png format. There is no actual analysis of data required through MatLab.
Further, the data files, though dealing with alterations of the same event, are not analysed against each other in data form. So, spending the time learning a new, faster method in MatLab for a task I need to complete in a week, when the c++ code file takes around 10 minutes to run for each file set and the MatLab code I have takes maybe 2 seconds to produce the graphs I require for each file set; the time required is not something I have the time at present to explore.
That is not to say it is not something I will not be exploring, but it is actually faster for me to manually change the file names myself each time than it would be to explore an alternative data structure (I believe).
I am happy to be corrected; I am the MatLab novice here after all. The C++ code generates three csv files; one called dtxxx, another is xyxxx and the third is xvxxxx. These deal with the data of two binary objects, the (x, y) position of a particle orbiting the binary system and the (x, Vx) of said particle where y = 0 and x < 0 (so, where the particle crosses the x-axis when x < 0). I need to plot the (x, y) graph (using the dt file to mark where the binary objects are) and the (x, Vx) plot - so, 2 plots per data set.
The data sets are based upon 2 variable changes; k = {0.9, 1.0, 1.1, 1.2, 1.3} and u = {-4, -6, -8} where r = k(u^(2/7)).
I might need to increase the range of k and/or u as I explore further.
I then drag and drop the generated files into MatLab to import them, run the graph code for each set of data files; exporting the generated plots as png files for consideration against each set.

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 25 Aug 2020
Edited: Stephen23 on 25 Aug 2020
Dragging-and-dropping into MATLAB actually makes your task harder, because it creates lots of separate variables. Drag-and-drop data importing is useful for one-off and investigative work, but not for anything that needs to be reliable or repeated.
You should write a small script to loop over the files, something like this (not complete, just to get you started):
D = 'absolute/relative path to the folder where the files are saved';
C = {'094','114','124','134','096','106','116','126','098','108','118','128','138'};
fgh = figure();
for k = 1:numel(C)
F = sprintf('dt%s.csv',C{k});
dt = readtable(fullfile(D,F));
scatter(dt.x,dt.y, '.');
... repeat for each CSV: import, plot, etc
... save figure
end
You can extend this loop yourself to import the other file/s and plot them in whatever way you need. Do not create new figures inside the loop, just replace data/update one figure.
Note that you might find it easier to simply use dir to get a list of the .csv files:
from which it wouldn't be hard to automatically generate that list of the unque filename suffixes. Or you could even generate the filenames directly from the values of k and u, if they are in some vectors.

More Answers (0)

Categories

Find more on Graphics Performance in Help Center and File Exchange

Tags

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!