How to automate the plotting of numerical data contained in Excel files?

7 visualizzazioni (ultimi 30 giorni)
I am running simulations that save different numerical outputs sorted into excell files in a folder. For each of these I would need to plot the numerical data automatically, saving some time.
Each excell file is set up as follows: the first line represents the x-axis while from the second line onward the y-axis. This means that if each file has e.g. 11 columns I would have to create 10 graphs for each pair of columns (the first and i-next from the second), and if possible group the graphs in one window for analysis purposes
I set up the importation phase of data as follows:
cartella = 'C:\Users\...';
files_excel = dir(fullfile(cartella, '*.csv')); % Puoi modificare '*.xlsx' a seconda dell'estensione dei tuoi file
tabelle = struct();
for i = 1:length(files_excel)
nome_file = fullfile(cartella, files_excel(i).name);
tabelle.(erase(files_excel(i).name, '.csv')) = readtable(nome_file);
fprintf('First 5 lines of %s:\n', files_excel(i).name);
disp(tabelle.(erase(files_excel(i).name, '.csv'))(1:5, :));
fprintf('--------------------------------------------\n');
end
Your assistance would be much appreciated
  2 Commenti
Austin M. Weber
Austin M. Weber il 11 Feb 2024
Do you want to plot the 10 graphs from each Excel file separately? Or would you like the 10 plots from each Excel file to be grouped together as subplots?
Les Beckham
Les Beckham il 11 Feb 2024
It looks like you are on the right track for reading the data. Making the plots shouldn't be to difficult to add, but the exact code to do so depends on the structure of the files and what you want the plots to look like. For instance, do the files have column headers that you want to retain as variable names in the tables? If not, you might be better off using readmatrix instead of readtable. Do you want all of your y data for each file plotted against the x data in one axis, or one axis for each y variable?
It would be easier to help with this if you provide a couple of example files and explain what you want the plots to look like.

Accedi per commentare.

Risposte (2)

Austin M. Weber
Austin M. Weber il 11 Feb 2024
Here is one way that you can make the 10 graphs for each of the excel files:
cartella = 'C:/.../'
files_excel = dir(fullfile(cartella,'*.xlsx'));
addpath(cartella)
for n = 1:length(files_excel)
T = readtable(files_excel(n).name);
figure(n)
for i = 2:11
subplot(4,3,i-1)
plot(T{:,1},T{:,i})
xlabel(['Column 1'])
ylabel(['Column ' num2str(i)])
end
sgtitle(files_excel(n).name)
end
The example above will loop through each of your excel files and make a figure that contains the 10 graphs from each of the files.

Star Strider
Star Strider il 11 Feb 2024
If you have R2018b or later, and each table contains data with numerical variables (excepting the first variable that is treated as the independent variable unless instructed otherwise in the code), you can use stackedplot with each table array. Each table plotted with stackedplot would need to create a new figure.

Categorie

Scopri di più su 2-D and 3-D Plots in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by