Defining table reference using list contents
2 views (last 30 days)
Show older comments
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
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:
Accepted Answer
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.
0 Comments
More Answers (0)
See Also
Categories
Find more on Graphics Performance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!