shifting a column in Matlab of CSV
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Hello , I have two CSV files attached in ZIP format.one of the colums is from -1 to 1.
Is the a way in matlab to import the CSV ,ater that shift -1 to 1 coulms into 0 to 2?
adding 1 to each cell of the column?
then saving the CSV in updated form?
Thanks.
0 Commenti
Risposte (2)
Drishti
il 7 Ott 2024
Modificato: Drishti
il 7 Ott 2024
Hi Fima,
To modify the range of the columns of the provided ‘.csv’ files from [-1,1] to [0,2], one possible work around is to first convert the table into an array form by leveraging ‘table2array’ function.
After converting the array to the ‘double’ data type, you can add `1` to each element. The modified array can be stored in the form of a table by utilizing ‘array2table’ function.
Refer to the code snippet below for better understanding:
% Read the CSV file into a table
data = readtable(filePath);
% Convert the table to an array for numerical operations
dataArray = table2array(data);
% Ensure the data array is of type double
dataArray = double(dataArray);
% Add 1 to each element in the array
dataArray = dataArray + 1;
% Convert any -0 to 0
dataArray(dataArray == 0) = 0;
% Convert the array back to a table
data = array2table(dataArray, 'VariableNames', data.Properties.VariableNames);
The above code modifies all the columns of the table. If you want to modify only a particular column, refer to the implementation below:
columnIndex = 2;
% Read the CSV file into a table
data = readtable(filePath);
% Add 1 to each element in the specified column
data{:, columnIndex} = data{:, columnIndex} + 1;
% Convert any -0 to 0 in the specified column
data{data{:, columnIndex} == 0, columnIndex} = 0;
You can also refer to the MATLAB Documentation of ‘table2array’ and ‘array2table’ functions:
I hope this helps in getting started.
0 Commenti
Star Strider
il 7 Ott 2024
The ‘Var1’ (first column) values are not exactly -1 and +1 so you have a choice to make. Either keep them as such (so after adding 1 the lowest values will be small floating-point decimal fractions) or use the round function to round them to integers. I have done both here, choose the approach you want.
Try this —
Uz = unzip('CSV_files.zip')
CH2 = readtable(Uz{1}) % Original
[Var1min, Var1max] = bounds(CH2.Var1) % Original
CH2.Var1 = CH2.Var1 + 1 % Add 1
[Var1min, Var1max] = bounds(CH2.Var1) % Check Result
CH2.Var1 = round(CH2.Var1) % Round To Nearest Integer
[Var1min, Var1max] = bounds(CH2.Var1) % Check Result
CH4 = readtable(Uz{1}) % Original
[Var1min, Var1max] = bounds(CH4.Var1) % Original
CH4.Var1 = CH4.Var1 + 1 % Add 1
[Var1min, Var1max] = bounds(CH4.Var1) % Check Result
CH4.Var1 = round(CH4.Var1) % Round To Nearest Integer
[Var1min, Var1max] = bounds(CH4.Var1) % Check Result
.
3 Commenti
Stephen23
il 7 Ott 2024
Star Strider
il 7 Ott 2024
If you give it the same name, that will over-write the existing file, so I am giving it a new name here —
Uz = unzip('CSV_files.zip')
CH4 = readtable(Uz{2}) % Original
CH4.Var1 = CH4.Var1 + 1 % Add 1
CH4.Var1 = round(CH4.Var1) % Round To Nearest Integer
writetable(CH4, 'ch4_2.csv') % Write To ‘.csv’ File
which('ch4_2.csv') % Check File Exists
% type('ch4_2.csv') % Check Results
The site is acting strangely today, and this is taking too long for it to run. The type call seems to be the problem. Everything else works.
.
Vedere anche
Categorie
Scopri di più su Data Type Conversion in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!