MATLAB: Defining table reference using list contents

listMATLAB

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.

Best Answer

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.