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

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월 19일
편집: Stephen23 2024년 3월 19일
"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.
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.
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 as I didn't quite understand what you mean by "how you're getting all of those variables into the workspace" :D
Thanks for your help!
"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개)

title(name)
If you want to plot the data need to be numeric arrays and not strings

댓글 수: 4

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.
I don't need to title my graph. I just need to change the name of the "name" variable in order to use it inside of the "plot" function. I've been trying with "sprintf", but as you said a string cannot be used inside "print". So my question is: what shall I use?
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
Sorry I wrote "print" but I meant "plot", my bad!

댓글을 달려면 로그인하십시오.

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

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 ;)
You're welcome! Good luck

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

릴리스

R2023b

질문:

2024년 3월 19일

댓글:

2024년 3월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by