Search code examples
matlabmatlab-compiler

How can I update calculated data in my excel sheet in the next column each time I run my code in MATLAB?


function []= process(f1, f2, f3, f4, height, th)

%%omitted the the large code from in between in order to just propose the problem%% 

ValuesInInches(12)=t1*t;
ValuesInInches(8)=realneck(f1,f2,height,th);
ValuesInInches(14)=t3*t; 
ValuesInInches(7)=t4*t;
ValuesInInches(11)= ValuesInInches(7);
ValuesInInches(5)=t5*t;
ValuesInInches(4)=t6*t;
ValuesInInches(6)=t7*t;
ValuesInInches(10)=t8*t;
ValuesInInches(9)=t9*t;
ValuesInInches(3)=t9*t1;
ValuesInInches(1)=t*t10;
ValuesInInches(2)=t11*t;
ValuesInInches(13)= measureknee(a5,t);
ValuesInInches=ValuesInInches';

file='measure.csv';

measurements{1,1}='Shirt Length';
measurements{2,1}='Full Shoulders';
measurements{3,1}='Sleeves';
measurements{4,1}='Muscle';
measurements{5,1}='Chest';
measurements{6,1}='Stomach';
measurements{7,1}='Hip';
measurements{8,1}='Neck';
measurements{9,1}='Trouser length';
measurements{10,1}='Trouser waist';
measurements{11,1}='Trouser hip';
measurements{12,1}='Thigh';
measurements{13,1}='Knee';
measurements{14,1}='Inseam';

T= table(measurements,ValuesInInches);
writetable(T,file);

end

I have to update my file measure.csv every time I make a change in my code in order to create a data-set of values. I am able to write data onto the file, but now i need that when my code runs for the second time, it writes the computed data onto the next column while keeping the old data safe. My code is either overwriting the data in the same column or i have to manually input the specific row column intersection location everytime.

My code computes 14 values every time it runs. Please tell me a way so that i can use it in my parent function to make the whole process more efficient.


Solution

  • Well first of all, if you are writing an Excel-Table I would rather recommend you using the function xlswrite(filename,Data,sheet,pos), which permits you to specifically set a value, vector or matrix into a specific position in a sheet you want.

    Second: If you can use a counter that follows up the amount of rows you have written, you can do it like this:

    function []= process(f1, f2, f3, f4, height, th, counter)
    % Your code...
    % suppose you want to write ValuesInInches into the next row
    
    strCounter = ('A':'Z'); % = 'ABC...Z'
    strPos = [strCounter(counter) '1']; % = 'B1' for instance
    xlswrite(file,ValuesInInches,strPos);
    
    end
    

    I hope this helps.