How to plot different subplots changing y variable name on each iteration

2 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2024-3-20
编辑:Stephen23 2024-3-20
"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.
Tommaso
Tommaso 2024-3-20
编辑:Tommaso 2024-3-20
I'll have a read at your link! I searched for that topic all the evening but clearly I was prompting the wrong things!!
I'll also have a try with "fieldnames" as I have 16 files and each contains 9 rows with 3/4 columns. Each row is one of the 9 subplots :D
(i know i should avoid, but that's part of an exercise, so i have to deal with it)
Thanks again, I'll have a try today!
EDIT: i understand that the link and documentation you provided me is the right way to do that, i agree and i would really like to do this that way. With this said: what i'm trying to do is part of an exercice and i cannot change the guideline on how i am supposed to do this. So i'll keep looking for a solution, thanks a lot again!

请先登录,再进行评论。

回答(2 个)

VBBV
VBBV 2024-3-19
title(name)
If you want to plot the data need to be numeric arrays and not strings
  4 个评论
VBBV
VBBV 2024-3-20
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

请先登录,再进行评论。


Voss
Voss 2024-3-21
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')
S = struct with fields:
data1_1_1: [9x4 double] data1_1_2: [9x4 double] data1_2_1: [9x4 double] data1_2_2: [9x4 double] data2_1_1: [9x4 double] data2_1_2: [9x4 double] data2_2_1: [9x4 double] data2_2_2: [9x4 double] data3_1_1: [9x4 double] data3_1_2: [9x4 double] data3_2_1: [9x4 double] data3_2_2: [9x4 double]
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 个评论
Tommaso
Tommaso 2024-3-21
That's what i wanted to do, untill i discovered i couldn't use structures unfortunately, the exercise is just like "find a direct (also if it is not smart) way to do this". I know this is no sense, but i have to deal with this i suppose ahahah!
Thanks for your help anyways ;)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Title 的更多信息

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by