Here are five lines that Matlab assembled when I plot my measurements. I need to create a function out of the average values from each line. I’m working with Matlab and I hope Matlab has an elegant solution for my problem. If not, I’ll take any advice.
Thanks so much in advance
Mike Here a small version of my code:
% Read text file
clc
clear all
%My Data is stored in that file.
fid = fopen(uigetfile('FILE.txt'), 'rt');
%The next lines are where I create my vectors, read the data, reshape my matrices etc
%I dont think you need to worry about it
%--------------------------------------------------------------------------
nrow = 16;
ncol = 10;
row_index = [9,10,11,12,13,14,15,16,8,7,6,5,4,3,2,1];
col_index = [1,2,3,4,5,6,7,8,9,10];
matAnzahl = 1;
line = '';
naechsterunde = 1;
while strcmp(line,'')
line = fgetl(fid);
C = textscan(line,'%f %f %f %f');
gewicht(matAnzahl) = C{1};
dots(matAnzahl) = C{2};
durchschnitt(matAnzahl) = C{3};
absolutdurchschnitt(matAnzahl) = C{4};
vector = fscanf(fid,'%u',160);
t = 1;
for i = 1:nrow
for j = 1:ncol
data2d(row_index(i), col_index(j)) = vector(t);
t = t + 1;
end
end
data2d;
fgetl(fid);
line = fgetl(fid);
if matAnzahl > 1
if gewicht(matAnzahl) < gewicht (matAnzahl-1)
naechsterunde = naechsterunde + 1;
end
end
matAnzahl = matAnzahl + 1;
end
matAnzahl = matAnzahl - 1;
%--------------------------------------------------------------------------
%This is where I create my y- vectors that are being ploted later on.
%y1-y5 are my results and I need to get the error value out of them and
%I need a function that replaces them
%Thank you
anzahl1 = 1;
for i = 1:matAnzahl/naechsterunde
y1(i) = absolutdurchschnitt(anzahl1);
if anzahl1 < matAnzahl
anzahl1 = anzahl1 + 1;
end
end
for i = 1:matAnzahl/naechsterunde
y2(i) = absolutdurchschnitt(anzahl1);
if anzahl1 < matAnzahl
anzahl1 = anzahl1 + 1;
end
end
for i = 1:matAnzahl/naechsterunde
y3(i) = absolutdurchschnitt(anzahl1);
if anzahl1 < matAnzahl
anzahl1 = anzahl1 + 1;
end
end
for i = 1:matAnzahl/naechsterunde
y4(i) = absolutdurchschnitt(anzahl1);
if anzahl1 < matAnzahl
anzahl1 = anzahl1 + 1;
end
end
for i = 1:matAnzahl/naechsterunde
y5(i) = absolutdurchschnitt(anzahl1);
if anzahl1 < matAnzahl
anzahl1 = anzahl1 + 1;
end
end
for i = 1:matAnzahl/naechsterunde
gewichtPlot(i) = gewicht(i);
i = i + 1;
end
plot(gewichtPlot,y1,gewichtPlot,y2,gewichtPlot,y3,gewichtPlot,y4,gewichtPlot,y5)
%--------------------------------------------------------------------------
For your question you can just replace your last line with:
plot(gewichtPlot,mean([y1; y2; y3; y4; y5]))
However, your code is written in a very "non-MATLAB" way, and I'll give you some examples:
First, you don't need to explicitly increase the iterator of a loop, it is done automatically. In fact, changing the loop iterator has no effect on the loop. So:
for i = 1:matAnzahl/naechsterunde
gewichtPlot(i) = gewicht(i);
i = i + 1;
end
can be just:
for i = 1:matAnzahl/naechsterunde
gewichtPlot(i) = gewicht(i);
end
Second, in this example you don't even need a loop, this line:
gewichtPlot(1:matAnzahl/naechsterunde) = gewicht(1:matAnzahl/naechsterunde);
will give the same result, because 1:matAnzahl/naechsterunde
already takes the same elements from the vectors. This is called "vectorization".
Here is another example. I'm not sure what your variables mean, but I'm quite sure that this code:
anzahl1 = 1;
for i = 1:matAnzahl/naechsterunde
y1(i) = absolutdurchschnitt(anzahl1);
if anzahl1 < matAnzahl
anzahl1 = anzahl1 + 1;
end
end
could be switched with:
y1(1:matAnzahl/naechsterunde) = absolutdurchschnitt(1:matAnzahl);
Keep in mind that it is not a good practice to use /
as the index, either in a loop or in a reference to a vector, because you have to be sure that the result is an integer.
Third, a better way to keep your data is in ordered matrices instead of different variables. So don't use y1
to y5
, each one in size 1-by-matAnzahl/naechsterunde
but just one y
in size 5-by-matAnzahl/naechsterunde
, and reference it as y(1,i)
to y(5,i)
. This way the last line of your code will be: plot(gewichtPlot,mean(y))
All the above was just to be general, in your case, there is a much simple way to write the whole second part, where you create all the y
vectors. Essentially
what you do there is just to divide the absolutdurchschnitt
vector into five equal pieces, and put them in y1
to y5
. So if we take example 3 from above, and the function reshape
that does the exact same thing we get:
absolutdurchschnitt = randi(100,1,15) % some arbitrary random data
y = reshape(absolutdurchschnitt,5,[]) % create y with all the data
plot(gewichtPlot,mean(y))
and this will yield:
absolutdurchschnitt =
Columns 1 through 11
37 63 86 13 18 96 98 26 29 84 81
Columns 12 through 15
37 37 28 26
y =
37 96 81
63 98 37
86 26 37
13 29 28
18 84 26
and some plot with the mean of y
by columns against gewichtPlot
.
There is much more to be done, but for a start, it will be good if you try to grasp these concepts.