I am struggling with structuring some code and sharing variables between functions and scripts.I want to use nested functions or a similar method but cannot resolve my issue so far. To allow you to understand my intent for the script I have created the same effect using global variables but I absolutely do not want to use these. This is only for illustrative purposes.
I have included xlsread in the main script so that it only runs once and creates data_matrix. The function meanfun then calculates the mean of every column within the data_matrix based on if data in column 2 a given speed1 value. The output is a 17xC matrix, which is perfect. I then use xls write to output the file the result to excel.
I only want to run xlswrite / read once due to the processing time.
I am only creating the structure here. I will need to create various other functions for std deviation and other analytics which will be output as well.
The files I am processing are sometimes large say (1000 columns x 100 rows) sometimes more. So efficiency is important for me.
I can't share the data but column 2 is filled of values matching (600:100:2200).
if true % code
input_file = 'inputdata_1.xlsx';output_file= 'outputdata_1.xlsx';global data_matrix speed1speed1 = (600:100:2200);data_matrix = xlsread(input_file);[r c] = size(data_matrix)n=2;m=3:1:c;res=meanfun(n,m);xlswrite(output_file,res);
here is the function I have created
if true % codeendfunction [ output ] = meanfun(ai,bi)%Function calculates the average of each column when the speed is located
%matches a value in the vector location specified.
global data_matrix speed1output = [mean(data_matrix(data_matrix(:,ai) == speed1(1), bi))mean(data_matrix(data_matrix(:,ai) == speed1(2), bi))mean(data_matrix(data_matrix(:,ai) == speed1(3), bi))mean(data_matrix(data_matrix(:,ai) == speed1(4), bi))mean(data_matrix(data_matrix(:,ai) == speed1(5), bi))mean(data_matrix(data_matrix(:,ai) == speed1(6), bi))mean(data_matrix(data_matrix(:,ai) == speed1(7), bi))mean(data_matrix(data_matrix(:,ai) == speed1(8), bi))mean(data_matrix(data_matrix(:,ai) == speed1(9), bi))mean(data_matrix(data_matrix(:,ai) == speed1(10), bi))mean(data_matrix(data_matrix(:,ai) == speed1(11), bi))mean(data_matrix(data_matrix(:,ai) == speed1(12), bi))mean(data_matrix(data_matrix(:,ai) == speed1(13), bi))mean(data_matrix(data_matrix(:,ai) == speed1(14), bi))mean(data_matrix(data_matrix(:,ai) == speed1(15), bi))mean(data_matrix(data_matrix(:,ai) == speed1(16), bi))mean(data_matrix(data_matrix(:,ai) == speed1(17), bi))]end
Best Answer