Hello,
I want to create a function that will take a data set, extract desired columns/rows , take their average and compute the difference.
I am new to this and I am not sure why I am getting errors, this is what I have so far. Does anyone know where I went wrong? I thought the function maybe should be the last line of my code, but that did not work either.
Also, as I have 20+ data sets I want to do this analysis on, can I do it on the same script or do I need a new different file?
Thank you!
%Create a functions that computes the averages of plateaus and difference between them.
function
data=readmatrix(x);
%extract desired rows and columns
x1=data(13911:111401,1);
y1=data(13911:111401,2);
x2=data(216399:1000015,1);
y2=data(216399:1000015,2);
Mx1= mean(x1,'all');
My1 = mean(y1,'all');
Mx2= mean(x2,'all');
My2 = mean(y2,'all');
mvd= (My2- My1);
end

Antworten (1)

Walter Roberson
Walter Roberson am 26 Mai 2021

0 Stimmen

function
data=readmatrix(x);
That needs to be on a single line.
Mx1= mean(x1,'all');
Using 'all' is not wrong, but it is unnecessary, as you are working with vectors.
Note that you compute the x means but you do not do anything with them.
Note that you do not return mvd, only data.

17 Kommentare

Macarena Santillan
Macarena Santillan am 26 Mai 2021
Thanks for your time, but I dont see how your answers is helpful
I am new to this and I am not sure why I am getting errors
You did not post a copy of the errors, so I had to analyze the syntax you gave. Your lines
function
data=readmatrix(x);
are syntactically wrong, and would need to be
function data=readmatrix(x);
This would overshadow the MATLAB function named "readmatrix", and would lead to problems slightly further in your code, as your code expects "data" to already exist.
Perhaps what you needed was instead
function NameOfYourFunctionGoesHere
data=readmatrix(x);
Macarena Santillan
Macarena Santillan am 27 Mai 2021
I tried your suggestions...
Do not separate the function keyword (line 3) and the rest of the function declaration (line 5). Delete line 5 and make the following line 3:
function data=readmatrix(x)
No, leave line 5 but change line 3 to
function computingmvdifference
Macarena Santillan
Macarena Santillan am 27 Mai 2021
Apparently the problem is on defining x, but I want to have a script maybe in where I say what x is (excel file) and the function will do it
function computingmvdifference(x)
and you would invoke with something like
computingmvdifference('Sepsis201907140815.xlsx')
You would replace 'Sepsis201907140815.xlsx' with the name of the file you wanted to read.
Macarena Santillan
Macarena Santillan am 27 Mai 2021
yeah thats the code i have already, but i have 20+ files to read so I wanted a function to make this fast,
Macarena Santillan
Macarena Santillan am 27 Mai 2021
This is what my normal code does, and it works fine just like that, but I have to be copy pasting the same thing over and over again and changing the name on line 46 to the appropiate array. And as I say I have at least 50 files of this every time I do an experiment, so I just want to make this easier
Walter Roberson
Walter Roberson am 27 Mai 2021
Bearbeitet: Walter Roberson am 27 Mai 2021
dinfo = dir('tek*.csv');
filenames = {dinfo.name};
for K = 1 : length(filenames)
computingmvdifference(filenames{K});
end
Macarena Santillan
Macarena Santillan am 27 Mai 2021
would you explain what you are attaching? as I mentioned, I am new to this so simple commands are not an explanation to me. I would like to understand how this stuff works
Walter Roberson
Walter Roberson am 27 Mai 2021
When you pass a pattern into dir(), the output is a struct array with information about each file it found that matches the pattern. In this particular case your example shows tek0023.csv so I guessed that you want to process all files whose name begin with 'tek' and have a csv extension, so pattern 'tek*.csv' is appropriate.
The struct array that is returned by dir() has a field named 'name' for each file. {dinfo.name} uses structure expansion, and is equivalent to having written {dinfo(1).name, dinfo(2).name, dinfo(3).name and so on to dinfo(end).name} for however many files were found. So afterwards filenames will be a cell array of character vectors, each of which is the name of one file that was found.
After that you just loop taking one file name at a time and passing it to computingvmdifference() to process.
Macarena Santillan
Macarena Santillan am 27 Mai 2021
thank you, how do i go about the loop?
Walter Roberson
Walter Roberson am 28 Mai 2021
You loop K according to the number of files, and you extract the current file and pass it to the function. I already posted the code.
Macarena Santillan
Macarena Santillan am 7 Jun. 2021
I am trying what you said and is still not working.... Why wont it read x?
dinfo = dir('tek*.csv');
filenames = {dinfo.name};
num_files = length(filenames);
results = zeros(num_files,5);
for K = 1 : length(filenames)
results(K,:) = computingmvdifference(filenames{K});
end
%Create a functions that computes the averages of plateaus and difference between them.
function results = computingmvdifference(x)
data = readmatrix(x);
%extract desired rows and columns
x1=data(13911:111401,1);
y1=data(13911:111401,2);
x2=data(216399:1000015,1);
y2=data(216399:1000015,2);
Mx1 = mean(x1,'all');
My1 = mean(y1,'all');
Mx2= mean(x2,'all');
My2 = mean(y2,'all');
mvd = (My2- My1);
results = [mvd, Mx1, My1, Mx2, My2];
end
After the code finishes, then
results(:,1) -- will be all of the mvd values
results(:,2) -- will be all of the Mx1 values
results(:,3) -- will be all of the My1 values
results(:,4) -- will be all of the Mx2 values
results(:,5) -- will be all of the My2 values
Macarena Santillan
Macarena Santillan am 7 Jun. 2021
Thank you so much! this works great!!

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Large Files and Big Data finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by