How to plot different subplots changing y variable name on each iteration
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
CONTEXT: I need to create 16 different figures and each has 9 subplots inside it. Each of this figures takes datas from a different dataset that is a 9x4 or 9x3 matrix.
PROBLEM: I can change the name of the dataset without problems using "for" loops, but in the moment I put the "name" variable (name of the dataset) inside the "plot" function I get this
"Invalid color, marker, or line style" error on the "plot" line
I must not change the dataset format or structure, I suppose the problem is due to me using "sprintf" but I have no clue on how to solve.
Thanks for your help!
The not-working part of the code looks like this (it is already inside of for loops that change the "c,k,j,l" variables):
name=sprintf("data%d_%d_%c(%d,:)",c,k,j,l);
figure(nfig)
subplot(3,3,l)
plot(fc,name)
imgName=sprintf("Number %d",l);
title(imgName)
grid on
4 Kommentare
Stephen23
am 20 Mär. 2024
Bearbeitet: Stephen23
am 20 Mär. 2024
"If I simply write the name of the variable it works. Problem is that I want it to work in a for loop, not writing every single of the 16 variable names."
Sure, I understand this. This is a very regular topic on this forum, and has been discussed at length many times before (so there is no point in me repeating it all again here):
The MATLAB documentation has a entire page dedicated to recommending that you should avoid doing this:
"Variables are imported in the workspace via the "load" function, if this ain't what you needed to know I have to ask you to be a little more specific..."
That is exactly what I was asking about. You should start by LOADing into an output variable rather than spamming all of that data willy-nilly into the workspace:
S = load(..);
What you should do then depends on how many variables there are in each MAT file (which you did not tell us):
- if there are multiple variables per file then use FIELDNAMES() and dynamic fieldnames,
- if there is one variable per file then use e.g. STRUCT2CELL() and simply access the 1st cell:
C = struct2cell(S);
M = C{1};
Now you have your imported data in M. Easy to work with, no magic names required. Storing and accessing such variables is trivial using indexing, exactly as the MATLAB documentation shows:
Note that better data design would not have meta-data in the variable names, which would then let you write simpler, more efficient, much more robust code. Keep that in mind when you design your own data one day.
Antworten (2)
Voss
am 21 Mär. 2024
If you have a mat file containing sequentially-numbered variables, e.g., data1_1_1, data1_1_2, etc., then you can load the mat file into a structure
S = load('data.mat')
and then the not-working part of your code can be easily adjusted to be made working (see https://www.mathworks.com/help/matlab/matlab_prog/generate-field-names-from-variables.html):
fc = 1:4;
nfig = 0;
for c = [1 2 3]
for k = [1 2]
for j = [1 2]
nfig = nfig+1;
figure(nfig)
name = sprintf("data%d_%d_%d",c,k,j);
M = S.(name);
for l = 1:9
subplot(3,3,l)
plot(fc,M(l,:))
imgName=sprintf("Number %d",l);
title(imgName)
grid on
end
end
end
end
2 Kommentare
Siehe auch
Kategorien
Mehr zu Line Plots finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!