Borrar filtros
Borrar filtros

Plotting tables stored in a struct

1 visualización (últimos 30 días)
Maciej Ejsmont
Maciej Ejsmont el 20 de Mayo de 2022
Editada: Jon el 20 de Mayo de 2022
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 comentarios
dpb
dpb el 20 de Mayo de 2022
"%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 el 20 de Mayo de 2022
Editada: Jon el 20 de Mayo de 2022
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.

Iniciar sesión para comentar.

Respuestas (0)

Categorías

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

Community Treasure Hunt

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

Start Hunting!

Translated by