Plot multiple variables from table with different x axes
12 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
Voss
2023-8-30
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
更多回答(3 个)
Dyuman Joshi
2023-8-30
编辑:Dyuman Joshi
2023-8-30
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
Read here - Access Data in a Tables
2 个评论
dpb
2023-8-30
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
David Hill
2023-8-30
figure;hold on;
for i=1:10
stairs(eval(sprintf('Map1.Distance_%1d',i)),eval(sprintf('Map1.Ca_%1d',i)));
end
1 个评论
dpb
2023-8-30
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'})
i=1;
stairs(Map1.(sprintf('Distance_%1d',i)),Map1.(sprintf('Ca_%1d',i)));
dpb
2023-8-30
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))
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!