Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-mini-2024-07-18· OpenAIAi2 WildChat-4.8M (AI2)

hey assistent I have this code if ~isempty(strfind(DTfilename, 'DTnew.xlsx'))…

  1. User

    hey assistent I have this code if ~isempty(strfind(DTfilename, 'DT_new.xlsx')) indice_V1000BV1=find(strcmp(raw(:,1),'V1000_BV1')); vhData.v1000=raw{indice_V1000BV1,2};

        indice_vehicleMass=find(strcmp(raw(:,1),'Curb weight (MVODM)'));
        indice_vehicleAdditionalMass=find(strcmp(raw(:,1),'Testing_additional_mass'));
        vhData.mass =raw{indice_vehicleMass,2}+raw{indice_vehicleAdditionalMass,2};
    
        indice_Energy=find(strcmp(raw(:,1),'Energy'));
        vhData.Energy=raw{indice_Energy,2};
    
        indice_Cylinders=find(strcmp(raw(:,1),'ICE_Cylinders_nb'));
        vhData.Cylinders=raw{indice_Cylinders,2};
    
        indice_Torque_max=find(strcmp(raw(:,1),'ICE_Torque_max'));
        vhData.Torque_max=raw{indice_Torque_max,2};
    
        indice_Rpm_torque_max=find(strcmp(raw(:,1),'ICE_Rpm_torque_max'));
        vhData.Rpm_torque_max=raw{indice_Rpm_torque_max,2};
    
        indice_Power_max=find(strcmp(raw(:,1),'ICE_Power_max'));
        vhData.Power_max=raw{indice_Power_max,2};
    
        indice_Maker=find(strcmp(raw(:,1),'Maker'));
        vhData.Maker=raw{indice_Maker,2};
    
        indice_Model=find(strcmp(raw(:,1),'Model'));
        vhData.Model=raw{indice_Model,2};
        
    elseif ~isempty(strfind(DTfilename, 'DT_2023.xls')) 
        
        indice_V1000BV1=find(strcmp(raw(:,1),'V1000_BV1'));
        vhData.v1000=raw{indice_V1000BV1,2};
    
        indice_vehicleMass=find(strcmp(raw(:,1),'Curb weight (MVODM)'));
        indice_vehicleAdditionalMass=find(strcmp(raw(:,1),'Testing_additional_mass'));
        if ~isnan(raw{indice_vehicleAdditionalMass,2})
            vhData.mass =raw{indice_vehicleMass,2}+raw{indice_vehicleAdditionalMass,2};
        else
            vhData.mass =raw{indice_vehicleMass,2}
        end
        indice_Energy=find(strcmp(raw(:,1),'Energy'));
        vhData.Energy=raw{indice_Energy,2};
    
        indice_Cylinders=find(strcmp(raw(:,1),'ICE_Cylinders_nb'));
        vhData.Cylinders=raw{indice_Cylinders,2};
    
        indice_Torque_max=find(strcmp(raw(:,1),'ICE_Torque_max'));
        vhData.Torque_max=raw{indice_Torque_max,2};
    
        indice_Rpm_torque_max=find(strcmp(raw(:,1),'ICE_rpm_Torque_max'));
        vhData.Rpm_torque_max=raw{indice_Rpm_torque_max,2};
    
        indice_Power_max=find(strcmp(raw(:,1),'ICE_Power_max'));
        vhData.Power_max=raw{indice_Power_max,2};
    
        indice_Maker=find(strcmp(raw(:,1),'Maker'));
        vhData.Maker=raw{indice_Maker,2};
    
        indice_Model=find(strcmp(raw(:,1),'Model'));
        vhData.Model=raw{indice_Model,2};
        indice_cylindree=find(strcmp(raw(:,1),'ICE_Capacity'));
        vhData.cylindree=raw{indice_cylindree,2};
        
        indice_Vmax=find(strcmp(raw(:,1),'Vmax'));
        vhData.Vmax=raw{indice_Vmax,2};
        
    elseif ~isempty(strfind(DTfilename, 'DT.xls')) 
        
        indice_V1000BV1=find(strcmp(raw(:,1),'V1000_BV1'));
        vhData.v1000=raw{indice_V1000BV1,2};
    
        indice_vehicleMass=find(strcmp(raw(:,1),'Vehicle_mass'));
        indice_vehicleAdditionalMass=find(strcmp(raw(:,1),'Vehicle_aditional_mass'));
        vhData.mass =raw{indice_vehicleMass,2}+raw{indice_vehicleAdditionalMass,2};
    
        indice_Energy=find(strcmp(raw(:,1),'Energy'));
        vhData.Energy=raw{indice_Energy,2};
    
        indice_Cylinders=find(strcmp(raw(:,1),'Cylinders'));
        vhData.Cylinders=raw{indice_Cylinders,2};
    
        indice_Torque_max=find(strcmp(raw(:,1),'Torque_max'));
        vhData.Torque_max=raw{indice_Torque_max,2};
    
        indice_Rpm_torque_max=find(strcmp(raw(:,1),'Rpm_torque_max'));
        vhData.Rpm_torque_max=raw{indice_Rpm_torque_max,2};
    
        indice_Power_max=find(strcmp(raw(:,1),'Power_max'));
        vhData.Power_max=raw{indice_Power_max,2};
    
        indice_Maker=find(strcmp(raw(:,1),'Maker'));
        vhData.Maker=raw{indice_Maker,2};
    
        indice_Model=find(strcmp(raw(:,1),'Model'));
        vhData.Model=raw{indice_Model,2};
    
        indice_Engine=find(strcmp(raw(:,1),'Engine'));
        vhData.Engine=raw{indice_Engine,2};
        
    end as you see is really complicated and ugly. can you make it less compelx
    
  2. Assistant

    Certainly! Your code has repeated logic for extracting data from the raw variable based on different file names. We can reduce redundancy by creating a dictionary or map of keys to their associated strings, looping through them, and handling the specific cases accordingly. Below is a refactored version of your code:

    % Define the mapping of expected keys for each filename
    keyMapping = containers.Map( ...
        {'DT_new.xlsx', 'DT_2023.xls', 'DT.xls'}, ...
        { ...
            {'V1000_BV1', 'Curb weight (MVODM)', 'Testing_additional_mass', 'Energy', ...
            'ICE_Cylinders_nb', 'ICE_Torque_max', 'ICE_Rpm_torque_max', ...
            'ICE_Power_max', 'Maker', 'Model'}, ...
            
            {'V1000_BV1', 'Curb weight (MVODM)', 'Testing_additional_mass', 'Energy', ...
            'ICE_Cylinders_nb', 'ICE_Torque_max', 'ICE_rpm_Torque_max', ...
            'ICE_Power_max', 'Maker', 'Model', 'ICE_Capacity', 'Vmax'}, ...
    
            {'V1000_BV1', 'Vehicle_mass', 'Vehicle_aditional_mass', 'Energy', ...
            'Cylinders', 'Torque_max', 'Rpm_torque_max', ...
            'Power_max', 'Maker', 'Model', 'Engine'} ...
        } ...
    );
    
    % Initialize vhData structure
    vhData = struct();
    
    % Determine which mapping to use based on the filename
    for i = 1:length(keyMapping.keys)
        if ~isempty(strfind(DTfilename, keyMapping.keys{i}))
            keys = keyMapping(keys{i});
            % Loop through each key and extract the corresponding data
            for j = 1:length(keys)
                key = keys{j};
                indice = find(strcmp(raw(:, 1), key));
    
                if ~isempty(indice)
                    if strcmp(key, 'Testing_additional_mass')
                        % Handle specific case for additional mass in DT_2023.xls
                        additional_mass = raw{indice, 2};
                        if strcmp(keyMapping.keys{i}, 'DT_2023.xls') && isnan(additional_mass)
                            vhData.mass = raw{find(strcmp(raw(:, 1), 'Curb weight (MVODM')), 2)};
                        else
                            vhData.mass = raw{find(strcmp(raw(:, 1), 'Curb weight (MVODM')), 2)} + additional_mass;
                        end
                    elseif strcmp(key, 'ICE_rpm_Torque_max') || strcmp(key, 'ICE_Cylinders_nb') || ...
                           strcmp(key, 'ICE_Torque_max') || strcmp(key, 'ICE_Power_max') || ...
                           strcmp(key, 'Maker') || strcmp(key, 'Model') || ...
                           strcmp(key, 'ICE_Capacity') || strcmp(key, 'Vmax') || ...
                           strcmp(key, 'Engine')
                        vhData.(replace(key, ' ', '_')) = raw{indice, 2}; % replace spaces with underscores for field names
                    else
                        vhData.v1000 = raw{find(strcmp(raw(:, 1), 'V1000_BV1')), 2};
                    end
                end
            end
            break; % Exit after handling the matched key mapping
        end
    end
    

    Key Changes:

    1. Mapping Structure: We created a containers.Map that associates file names with their respective set of expected keys.

    2. Loops: The code now uses loops to handle repetitive tasks, thus eliminating redundancy.

    3. Dynamic Field Assignment: By using dynamic field references (e.g., vhData.(replace(key, ' ', '_'))), we can assign values to vhData based on the key names in the mapping.

    4. Handling Special Cases: Special cases (like additional mass) are checked with clear conditions.

    This refactored code should be easier to maintain and understand while keeping the same functionality!

  3. User

    I think you made even more complecated. You can still make it with if else

  4. Assistant

    Sure! Let's simplify the code while still using if-else statements without introducing more complexity. We can reduce redundancy and improve readability by using a helper function to get the values for vhData. Here's a revised version of your original code using basic if-else structures while minimizing repetition:

    % Initialize vhData structure
    vhData = struct();
    
    % Define a helper function to get data from raw based on key
    function value = getData(raw, key)
        index = find(strcmp(raw(:, 1), key));
        if ~isempty(index)
            value = raw{index, 2};
        else
            value = NaN; % Or handle this case as needed
        end
    end
    
    % Check the filename and extract data
    if contains(DTfilename, 'DT_new.xlsx')
    
        vhData.v1000 = getData(raw, 'V1000_BV1');
        mass = getData(raw, 'Curb weight (MVODM')) + getData(raw, 'Testing_additional_mass');
        vhData.mass = mass;
    
        vhData.Energy = getData(raw, 'Energy');
        vhData.Cylinders = getData(raw, 'ICE_Cylinders_nb');
        vhData.Torque_max = getData(raw, 'ICE_Torque_max');
        vhData.Rpm_torque_max = getData(raw, 'ICE_Rpm_torque_max');
        vhData.Power_max = getData(raw, 'ICE_Power_max');
        vhData.Maker = getData(raw, 'Maker');
        vhData.Model = getData(raw, 'Model');
    
    elseif contains(DTfilename, 'DT_2023.xls')
    
        vhData.v1000 = getData(raw, 'V1000_BV1');
    
        vehicleMass = getData(raw, 'Curb weight (MVODM'));
        additionalMass = getData(raw, 'Testing_additional_mass');
        if ~isnan(additionalMass)  % Check for NaN
            vhData.mass = vehicleMass + additionalMass;
        else
            vhData.mass = vehicleMass;
        end
    
        vhData.Energy = getData(raw, 'Energy');
        vhData.Cylinders = getData(raw, 'ICE_Cylinders_nb');
        vhData.Torque_max = getData(raw, 'ICE_Torque_max');
        vhData.Rpm_torque_max = getData(raw, 'ICE_rpm_Torque_max');
        vhData.Power_max = getData(raw, 'ICE_Power_max');
        vhData.Maker = getData(raw, 'Maker');
        vhData.Model = getData(raw, 'Model');
        vhData.cylindree = getData(raw, 'ICE_Capacity');
        vhData.Vmax = getData(raw, 'Vmax');
    
    elseif contains(DTfilename, 'DT.xls')
    
        vhData.v1000 = getData(raw, 'V1000_BV1');
        vhData.mass = getData(raw, 'Vehicle_mass') + getData(raw, 'Vehicle_aditional_mass');
        vhData.Energy = getData(raw, 'Energy');
        vhData.Cylinders = getData(raw, 'Cylinders');
        vhData.Torque_max = getData(raw, 'Torque_max');
        vhData.Rpm_torque_max = getData(raw, 'Rpm_torque_max');
        vhData.Power_max = getData(raw, 'Power_max');
        vhData.Maker = getData(raw, 'Maker');
        vhData.Model = getData(raw, 'Model');
        vhData.Engine = getData(raw, 'Engine');
    
    end
    

    Key Modifications:

    1. Helper Function: A helper function getData encapsulates the logic for retrieving values from raw, reducing repetitive code.

    2. Use of contains: The contains function provides better readability compared to using isempty(strfind(...)).

    3. Simplified Logic: The conditional logic is contained within the specific if-else blocks but has been simplified to use the helper function.

    This should maintain clarity while achieving a reduction in code duplication and complexity.