Plotting multiple data on one graph and plotting the average of all the files on the same graph.

2 views (last 30 days)
I have say 25 mat files ( one "x" data and two "y" data) stored in a structure. I want to scatter plot all the data in the 25 mat files using the "x' data and one "y" value on one graph and plot the average on the same graph.
The path to my x data is: x = File.Header.AVC'; % I transpose the data to have them in column
and the y1 = File.EnergyMeasu_00516.AVG.pol1';
The digits 00516 changes for each file. I saw a similar answer here and I tried but it's not working.
This is what I tried so far.
d = dir('EnergyMeasu*.mat');
NoF = length(d);
for j = 1:25
filename = d(j).name;
load(filename)
x = File.Header.AVC'
y1 = File.filename.AVG.pol1'; % I replaced the "EnergyMeasu_00516" since the digits is different for each file. This has change the path since the file name has an extension .mat. I checked previous answers to take the extension off but it did not work.
figure(1)
plot (x,y1,'Linewidth',2);
hold on
end
M = cat(25,C{:})
Average = mean(M,25)
y_average = average[:,2]
plot (x,y_average,'Linewidth',2); % The X value is fixed so I only need to find the average of all y1 in the files.
hold off;
ylim([-0.005 0.08]);
xlim([829 855])
xlabel("Energy eV");
ylabel("Voltage V");
title("EnergyMeasu");
  3 Comments
Richard
Richard on 22 Dec 2022
Hello KSSV. Thank you for responding. This is the error I get when I run the code .......>
Index exceeds the number of array elements. Index must not exceed 0.
Error in untitled6 (line 4)
filename = d(j).name;
Mathieu NOE
Mathieu NOE on 22 Dec 2022
Edited: Mathieu NOE on 22 Dec 2022
hello
your code would be more robust if instead of hard coding 25 all over the place you would use the identified number of files (NoF) that you are not using in your code
d = dir('EnergyMeasu*.mat');
NoF = length(d);
for j = 1:NoF
filename = d(j).name;
load(filename)
x = File.Header.AVC'
y1 = File.filename.AVG.pol1'; % I replaced the "EnergyMeasu_00516" since the digits is different for each file. This has change the path since the file name has an extension .mat. I checked previous answers to take the extension off but it did not work.
figure(1)
plot (x,y1,'Linewidth',2);
hold on
end
M = cat(NoF,C{:})
Average = mean(M,NoF)
y_average = average[:,2]
plot (x,y_average,'Linewidth',2); % The X value is fixed so I only need to find the average of all y1 in the files.
hold off;
ylim([-0.005 0.08]);
xlim([829 855])
xlabel("Energy eV");
ylabel("Voltage V");
title("EnergyMeasu");

Sign in to comment.

Answers (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 23 Dec 2022
Here is your code with some corrections by presuming that the size of y1 is the same from all data files:
...
for j = 1:NoF
...
y1 = File.filename.AVG.pol1'; % I replaced the "EnergyMeasu_00516" since the digits is different for each file. This has change the path since the file name has an extension .mat. I checked previous answers to take the extension off but it did not work.
Y1(j,:)=y1; % ALL data in y1 are saved inside one variable
...
end
Y1_ave = mean(Y1); % Averaged y1 values
...

Community Treasure Hunt

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

Start Hunting!