Plotting tables stored in a struct

조회 수: 1 (최근 30일)
Maciej Ejsmont
Maciej Ejsmont 2022년 5월 20일
편집: Jon 2022년 5월 20일
Hi, i am trying to plot several tables that are stored in a structure. I was switching between storing:
  • tables as indices of one field
for i=1:5
clear("forwardTable")
%Code that generates forwardTable from imported files not included,
%forward tables has the same columns, but can have different number of rows for each file
finalData(i).dataTable = forwardTable;
plotBig(i) = plot(finalData(i).dataTable,"time / min","voltage / V");
hold on
end
hold off
  • or tables as seperate fields
names = string{'a','b','c','d','e'} % I couldn't get proper string array with names = ('ble' 'ble'), ['ble' 'ble'] or {'ble' 'ble'}
for i=1:5
clear("forwardTable")
%Code that generates forwardTable from imported files not included,
%forward tables has the same columns, but can have different number of rows for each file
finalData.(names(i)) = forwardTable;
plotBig(i) = plot(finalData.(names(i)),"time / min","voltage / V");
hold on
end
hold off
As you can see i am plotting at each step of the for loop, but plotting in a loop is time consuming. Is there a way, to plot in one line of code entire contents of the structure outside the loop? Something like this (both don't work):
plot(finalData([1:5]).dataTable,"time / min","voltage / V")
%or
plot(finalData.(names(1:5)),"time / min","voltage / V")
  댓글 수: 2
dpb
dpb 2022년 5월 20일
"%Code that generates forwardTable from imported files not included, "
There's the problem -- we can't really visualize the problem w/o the ability to see the data file structure to have some idea of what could be the more efficient way to go at the problem.
Building complex data structures of any sort is time-consuming and makes for more obfuscated code to address so looking at the beginning to try to avoid creating the problem you're trying to solve would be the first step.
Attach a sample file that illustrates what the data are that are trying to plot and also explain just what the plot you're trying to create would be...
Jon
Jon 2022년 5월 20일
편집: Jon 2022년 5월 20일
I don't immediately know of a way to plot all the data from your array of tables in just one line without loops. Although it still would not solve this problem, in general I would suggest that it is better to "stack" your tables into one large table rather than making an array of tables. So take your current current table and add one more column that givse the testNumber (or other unique id) and stack the tables vertically. Then if you just want to plot the data from test 3 you can use logical indexing on the testNo to find all of the rows in the table with the corresponding test number in the "testNumber" column. There are also lots of grouping tools in Matlab that could be applied.

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

답변 (0개)

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by