Plot multiple variables from table with different x axes

12 visualizaciones (últimos 30 días)
Hannah Cunningham
Hannah Cunningham el 30 de Ag. de 2023
Respondida: dpb el 30 de Ag. de 2023
Hello!
I have a table where I have multiple (300+) different transect measurements, so I have columns named Distance_1, Ca_1, Fe_1, Distance_2, Ca_2, etc. I am wanting to plot multiple Ca columns against their corresponding Distance column but I cannot find a way to do it without doing each individually.
I can't figure out how I would loop through the columns since when I try to do something like
for i=1:10
h=stairs(Map1.Distance_i,Map1.Ca_i);
hold on
end
it says there is unrecognized table variable "Distance_i". I do have the columns in order, so it always goes Distance-Mn-Ca-Fe-Mg-XMg, so the next 'Distance' column is always n+6 but I run into the same issue with unrecognized table variables.
Thanks so much.

Respuesta aceptada

Voss
Voss el 30 de Ag. de 2023
ncol = size(Map1,2);
for i=1:ncol/6
h=stairs(Map1.(sprintf('Distance_%d',i)),Map1.(sprintf('Ca_%d',i)));
hold on
end
Or:
ncol = size(Map1,2);
for i=1:ncol/6
h=stairs(Map1{:,sprintf('Distance_%d',i)},Map1{:,sprintf('Ca_%d',i)});
hold on
end

Más respuestas (3)

Dyuman Joshi
Dyuman Joshi el 30 de Ag. de 2023
Editada: Dyuman Joshi el 30 de Ag. de 2023
When you do this
h=stairs(Map1.Distance_i,Map1.Ca_i);
MATLAB looks for the column named Distance_i and Ca_i in the Map1, which do not exist, and thus gives the error you received.
To access the data, you can generate column names as strings and use them -
for i=1:10
t1=sprintf('Distance_%d\n',i);
t2=sprintf('Ca_%d',i);
h=stairs(Map{:,t1},Map{:,t2});
hold on
end
And since the columns in the table are arranged in an order, you can use indexing as well -
for i=1:10
h=stairs(Map.(1+6*(i-1)),Map.(3+6*(i-1)));
end
  2 comentarios
dpb
dpb el 30 de Ag. de 2023
Alternate table addressing syntax -- use variable name with .(x) where x is a variable holding the desired variable. The "." returns the whole referenced column without the need for the explicit 2D indexing and the curlies "{}".
@Dyuman Joshi used the same syntax with the the numeric indices, but the more involved one with the variable names...
hold on
for i=1:10
D=compose('Distance_%d',i);
C=compose('Ca_%d',i);
stairs(Map.(D),Map.(C))
end
Dyuman Joshi
Dyuman Joshi el 30 de Ag. de 2023
Force of habit ¯\_(ツ)_/¯

Iniciar sesión para comentar.


David Hill
David Hill el 30 de Ag. de 2023
figure;hold on;
for i=1:10
stairs(eval(sprintf('Map1.Distance_%1d',i)),eval(sprintf('Map1.Ca_%1d',i)));
end
  1 comentario
dpb
dpb el 30 de Ag. de 2023
There's no need for eval here...just reference the table variable dynamically instead of the table itself---you have to code the variable name in somewhere, anyway...
Map1=array2table([[0:4]' rand(5,1)],'VariableNames',{'Distance_1','Ca_1'})
Map1 = 5×2 table
Distance_1 Ca_1 __________ _______ 0 0.61311 1 0.81437 2 0.14077 3 0.39314 4 0.64307
i=1;
stairs(Map1.(sprintf('Distance_%1d',i)),Map1.(sprintf('Ca_%1d',i)));

Iniciar sesión para comentar.


dpb
dpb el 30 de Ag. de 2023
There's also no need to loop; use the array inputs version of stairs
D_indices=[1:3:width(map1)]; % would be all; limit to what desired
C_indices=D_indices+1; % Ca follows D in order
stairs(map1{:,D_indices},map1{:,C_indices}) % stairs with arrays X,Y
NOTA BENE: Above uses the indexing; could build the variable names(*), but the syntax with "dot" notation only works with a single variable so it doesn't help any.
(*) More efficient, simpler than rebuilding variable names from string operations is to simply extract them from the Properties.VariableNames of the table. For example, the above could be followed by
lengend(map1.Properties.VariableNames(C_indices))

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by