Plot loop n variables

3 visualizaciones (últimos 30 días)
Jorge Rodriguez
Jorge Rodriguez el 17 de Ag. de 2017
Editada: Jan el 18 de Ag. de 2017
Hello Community,
I have been trying to create a plot of several arrays in loops, but I have problems assigning the change of name of the variable to the plot. Also if you know how to assign the legend name according to the plot it would be great. So, I have A1,A2,A3,... and B1,B2,B3,... respectively each pair makes a plot. So far I have written: Arrays A, B are a size (5x10000), and I'm trying to plot the second column of A (date) with the second column of B with:
%nrows is the number of pairs of A and B
for i=1:nrows
x=datetime(A{i}(:,2)', 'ConvertFrom', 'datenum');
y=B{i}(:,2)';
figure
plot(x,y);
xlabel('A');ylabel('B'); grid on; grid minor; legend ('B',num2str(i));
hold on
end
The error that I get is "Undefine variable A or Class A"
Any help is very appreciated. Thanks
  5 comentarios
Image Analyst
Image Analyst el 18 de Ag. de 2017
I'm wondering if per is right and he just has two cell arrays A and B, and when he wrote A1, A2, etc. he really meant A(1}, A{2}, etc. In fact his code tries to reference A like that, but it appears the problem is that A was never defined/created/passed-in in the first place.
Jorge Rodriguez
Jorge Rodriguez el 18 de Ag. de 2017
Thanks for the suggestions. As you can figure out from my code. I'm new in MATLAB or coding in general, that the reason I ask.

Iniciar sesión para comentar.

Respuesta aceptada

Sebastian Castro
Sebastian Castro el 18 de Ag. de 2017
Editada: Sebastian Castro el 18 de Ag. de 2017
This is happening because the syntax A{1} doesn't mean you're accessing a variable named A1. It's accessing the first element of a variable named A.
To put John D'Errico's suggestions in a ... more positive way, the 2 syntaxes above show the difference in stringing together things in a single array vs. having to manage a bunch of hard-coded variable names like A1, A2, etc. The former approach ends up being better to work with because it's less manual effort.
Based on your previous question, if you used accumarray to split your original data you would get a cell array. This is a way to store data of non-uniform size and/or types, which was the way to handle splitting your particular dataset. If you used that approach, you can then access each individual array as A{1}, A{2}, etc.
- Sebastian
  1 comentario
Jorge Rodriguez
Jorge Rodriguez el 18 de Ag. de 2017
Thanks Sebastian, I will try to implement this format.

Iniciar sesión para comentar.

Más respuestas (1)

John BG
John BG el 18 de Ag. de 2017
Hi Mr Rodriguez
while it's a good general guideline to try to work with variables that use matrix indices instead of putting the index in the name of the variable, it's a common practice, it happens, and since you already have the data in such format, let's work with what we've go available:
1.
Let's say you have the following
A1 =
1.4897 1.4172
1.4090 0.6715
>> A2
A2 =
-1.2075 1.6302
0.7172 0.4889
>> A3
A3 =
1.0347 -0.3034
0.7269 0.2939
>> B1
B1 =
-0.8095 1.4384
-2.9443 0.3252
>> B2
B2 =
-0.7549 -1.7115
1.3703 -0.1022
>> B3
B3 =
-0.2414 0.3129
0.3192 -0.8649
2.
now capture the names of the variables of interest
S=whos
S =
10×1 struct array with fields:
name
size
bytes
class
global
sparse
complex
nesting
persistent
S.name
ans =
'A'
ans =
'A1'
ans =
'A2'
ans =
'A3'
ans =
'AB'
ans =
'B1'
ans =
'B2'
ans =
'B3'
ans =
'S'
ans =
'ans'
3.
Now have the choice to either
3.1.- work with the names of the variables contained in, for instance
D={S.name}
D =
1×10 cell array
'A1' 'A2' 'A3' 'AB' 'B1' 'B2' 'B3' 'D' 'S' 'ans'
there may not be need for a cell, but if you have more than 9 pairs A B then the length of the variable names varies with the increasing numerals
3.2.- or use the code you have already included in your question
A={}
for k=1:1:length(D)
if findstr(D{k},'A')
A=[A;D{k}]
end
end
B={}
for k=1:1:length(D)
if findstr(D{k},'B')
B=[B;D{k}]
end
end
now you can start working on the plots mentioned in the question, or any other processing
if you find this answer useful would you please be so kind to consider marking my answer as Accepted Answer?
To any other reader, if you find this answer useful please consider clicking on the thumbs-up vote link
thanks in advance
John BG
  5 comentarios
John BG
John BG el 18 de Ag. de 2017
Editada: John BG el 18 de Ag. de 2017
it will be 'long' as in 'langsam' if the name of the variable is complicated and there are many complicated variable names.
But how complicated and slow can it be to retrieve A1 A2 .. and B1 B2 ..
People come on, when Coberdick and Simon say 'slow' without bringing any time measurement, what's the point?
Jan
Jan el 18 de Ag. de 2017
Editada: Jan el 18 de Ag. de 2017
"Slow" means, that using arrays instead of a set of variables with numbered names will be "faster" - measured in runtime and taking into account the the time for programming, debugging and maintaining the code.

Iniciar sesión para comentar.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by