Interpolation .csv file & create new interpolated .csv file

Version 1.0.1 (1.45 KB) by Thang
Providing a brief describe and code for interpolating from the .csv file
1 Download
Updated 25 Oct 2024

View License

I'm here to provide an efficient tool for interpolation variables from a .csv file and then create a new .csv file and import the new interpolated values into it.
Here are the step-by-step instructions, don't skip these parts.
Step 1: Load CSV file with 'preserve' rule to keep the original column names. And dont forget to replace the name yourfile.csv with your actual .csv file name.
data = readtable('yourfile.csv', 'VariableNamingRule', 'preserve'); % Replace 'yourfile.csv' with the actual file name
Step 2: Check the actual column names in the data
disp(data.Properties.VariableNames); % Display column names to confirm
For example, I'm doing with the two columns which equivalent with two variables (independent and dependent variable)
Assuming the CSV has columns named 'Wavelength' and 'Diffuse Reflectance'
wavelength = data.('Wavelength (nm)'); % Use the correct column name from your file
diffuse_reflectance = data.('Reflectance (unitless)'); % Use the correct column name
Here, the Wavelength is an independent variable and the Diffuse_Reflectance is a dependent variable
Step 3: Define new wavelength range with step size of 1
new_wavelength = min(wavelength):1:max(wavelength);
This code is to create new wavelength list with the step size between value to value is 1
For example, I have here my .csv file before interpolating
And this is my new .csv file after interpolating...See the difference??
It has more points which is so-called smoother!
Step 4: Interpolate Diffuse Reflectance at new wavelength values
new_diffuse_reflectance = interp1(wavelength, diffuse_reflectance, new_wavelength, 'linear');
new_wavelength = floor(new_wavelength);
Here I'm using the floor() function is to take the value without decimal point...But I'm taking the nearest integer not round it up. For example, The wavelength 901.975575 which has the floor() = 901, but has the round() = 902. So you could use the round() function to round your value up without taking any number after decimal point.
Up to now, you are done in processing your .csv file, now is to create a new one and insert new interpolated values into it.
Step 5: Create new table with interpolated data
new_data = table(new_wavelength', new_diffuse_reflectance', 'VariableNames', {'Wavelength', 'Reflectance (unitless)'});
Step 6: Save new table to a CSV file
writetable(new_data, 'interpolated_yourfile.csv');
Finally, your new interpolated .csv file has the name above, you could change it anyway.
Full code here:
% Step 1: Load CSV file with 'preserve' rule to keep the original column names
data = readtable('yourfile.csv', 'VariableNamingRule', 'preserve'); % Replace 'yourfile.csv' with the actual file name
% Step 2: Check the actual column names in the data
disp(data.Properties.VariableNames); % Display column names to confirm
% Assuming the CSV has columns named 'Wavelength' and 'Diffuse Reflectance'
wavelength = data.('Wavelength (nm)'); % Use the correct column name from your file
diffuse_reflectance = data.('Reflectance (unitless)'); % Use the correct column name
% Step 3: Define new wavelength range with step size of 1
new_wavelength = min(wavelength):1:max(wavelength);
% Step 4: Interpolate Diffuse Reflectance at new wavelength values
new_diffuse_reflectance = interp1(wavelength, diffuse_reflectance, new_wavelength, 'linear');
new_wavelength = floor(new_wavelength);
% Step 5: Create new table with interpolated data
new_data = table(new_wavelength', new_diffuse_reflectance', 'VariableNames', {'Wavelength', 'Reflectance (unitless)'});
% Step 6: Save new table to a CSV file
writetable(new_data, 'interpolated_yourfile.csv');

Cite As

Thang (2025). Interpolation .csv file & create new interpolated .csv file (https://in.mathworks.com/matlabcentral/fileexchange/174445-interpolation-csv-file-create-new-interpolated-csv-file), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2023b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!
Version Published Release Notes
1.0.1

Minor bugs

1.0.0