function battery_analysis(filename,Fs, 1, 2, 3, R_shunt, metadata)
%real files and set columns
data = readmatrix(filename);
V_batt = data(:,1);
V_shunt = data(:,2);
 if isempty(3)
    Temp = [];
  else
    Temp =data(:,3);
 end
%set time
Fs = 9600;
N = length(data);
t = (0:N-1)/Fs;
%Equations 
I = V_shunt/R_shunt;
Power = V_batt .*I;
capacity = trapz(t,I)/3600;
energy = trapz(t,Power)/3600;
 0;
 discharge = I<0;
charge = I > 0;
capacity_charge = trapz(t(charge),I(charge));
capacity_discharge = trapz(t(discharge),I(discharge));
%store
results.time = t;
results.voltage = V_batt;
results.current = I;
results.power = power;
results.temperature = Temp;

results.capacity_Ah = capacity;
results.energy_Wh = energy;
results.charge_capacity_Ah = charge;
results.discharge_capacity_Ah = discharge;

results.metadata = metadata;
     end
 
function save_battery_cycle(root_folder, cycle_number, filename, Fs, voltage_col, shunt_col, temp_col, R_shunt, metadata)

        % --- 1. Create folder structure ---
        cycle_name = sprintf("Cycle_%03d", cycle_number);
        cycle_path = fullfile(root_folder, cycle_name);

        if ~exist(cycle_path, 'dir')
            mkdir(cycle_path);
        end

        plot_path = fullfile(cycle_path, "plots");
        if ~exist(plot_path, 'dir')
            mkdir(plot_path);
        end

        % --- 2. Run analysis ---
        results = analyze_battery_cycle(filename, Fs, 1, 2, 3, R_shunt, metadata);

        % --- 3. Save MAT file ---
        save(fullfile(cycle_path, "analysis.mat"), "results");

        % --- 4. Save JSON summary ---
        summary = struct();
        summary.cycle = cycle_number;
        summary.battery_id = metadata.battery_id;
        summary.profile = metadata.profile;
        summary.timestamp = metadata.timestamp;
        summary.temp_enabled = metadata.temp_enabled;

        summary.capacity_Ah = results.capacity_Ah;
        summary.energy_Wh = results.energy_Wh;
        summary.charge_capacity_Ah = results.charge_capacity_Ah;
        summary.discharge_capacity_Ah = results.discharge_capacity_Ah;

        summary.Fs = Fs;
        summary.R_shunt = R_shunt;

        json_text = jsonencode(summary);
        fid = fopen(fullfile(cycle_path, "summary.json"), 'w');
        fwrite(fid, json_text, 'char');
        fclose(fid);

        % --- 5. Save plots ---
        figure;
        subplot(3,1,1);
        plot(results.time, results.voltage); ylabel('Voltage (V)');
        title('Battery Voltage');

        subplot(3,1,2);
        plot(results.time, results.current); ylabel('Current (A)');
        title('Battery Current');

        subplot(3,1,3);
        plot(results.time, results.power); ylabel('Power (W)'); xlabel('Time (s)');
        title('Battery Power');

        saveas(gcf, fullfile(plot_path, "voltage_current_power.png"));

        % 6. Optional temperature plot 
        if ~isempty(results.temperature)
            figure;
            plot(results.time, results.temperature);
            ylabel('Temperature (°C)');
            title('Cell Temperature');
            saveas(gcf, fullfile(plot_path, "temperature.png"));
        end

    end
