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

5 Ansichten (letzte 30 Tage)
Tommaso
Tommaso am 19 Mär. 2024
Kommentiert: Voss am 21 Mär. 2024
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
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.
Tommaso
Tommaso am 20 Mär. 2024
Bearbeitet: Tommaso am 20 Mär. 2024
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!

Melden Sie sich an, um zu kommentieren.

Antworten (2)

VBBV
VBBV am 19 Mär. 2024
title(name)
If you want to plot the data need to be numeric arrays and not strings
  4 Kommentare
VBBV
VBBV am 20 Mär. 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
Tommaso am 20 Mär. 2024
Sorry I wrote "print" but I meant "plot", my bad!

Melden Sie sich an, um zu kommentieren.


Voss
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')
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 Kommentare
Tommaso
Tommaso am 21 Mär. 2024
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 ;)

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Line Plots finden Sie in Help Center und File Exchange

Produkte


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by