How to plot different subplots changing y variable name on each iteration
Show older comments
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 Comments
"I suppose the problem is due to me using "sprintf" but I have no clue on how to solve."
No, the problem is how you are getting all of those variables into the workspace. But you forgot to tell us this most important information. If you tell us this, then we can help you to write better code.
By far the best solution is to fix your code where that data is imported into the workspace.
Tommaso
on 20 Mar 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.
Answers (2)
VBBV
on 19 Mar 2024
title(name)
If you want to plot the data need to be numeric arrays and not strings
4 Comments
VBBV
on 19 Mar 2024
Use title function for the subplot and plot the numeric data of the variable you want to graph. The outputs from sprintf function are usually strings or charcters which cannot be used inside the plot function.
Tommaso
on 20 Mar 2024
VBBV
on 20 Mar 2024
but as you said a string cannot be used inside "print"
No, it cannot be used inside plot function. Please attach sample data of subplots and/or your code
Tommaso
on 20 Mar 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
Categories
Find more on Loops and Conditional Statements 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!










