shifting a column in Matlab of CSV

1 回表示 (過去 30 日間)
fima v
fima v 2024 年 10 月 7 日
コメント済み: Star Strider 2024 年 10 月 7 日
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.

回答 (2 件)

Drishti
Drishti 2024 年 10 月 7 日
編集済み: Drishti 2024 年 10 月 7 日
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.

Star Strider
Star Strider 2024 年 10 月 7 日
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')
Uz = 1x2 cell array
{'ch2.csv'} {'ch4.csv'}
CH2 = readtable(Uz{1}) % Original
CH2 = 500002x2 table
Var1 Var2 ________ _____ -1 -0.18 -1 0.02 -0.99999 -0.18 -0.99999 -0.16 -0.99999 -0.27 -0.99998 -0.09 -0.99998 -0.05 -0.99997 -0.19 -0.99997 -0.01 -0.99997 -0.14 -0.99996 -0.25 -0.99996 -0.05 -0.99995 -0.11 -0.99995 0 -0.99995 -0.17 -0.99994 -0.05
[Var1min, Var1max] = bounds(CH2.Var1) % Original
Var1min = -1.0000
Var1max = 1.0000
CH2.Var1 = CH2.Var1 + 1 % Add 1
CH2 = 500002x2 table
Var1 Var2 _______ _____ -2e-06 -0.18 2e-06 0.02 6e-06 -0.18 1e-05 -0.16 1.4e-05 -0.27 1.8e-05 -0.09 2.2e-05 -0.05 2.6e-05 -0.19 3e-05 -0.01 3.4e-05 -0.14 3.8e-05 -0.25 4.2e-05 -0.05 4.6e-05 -0.11 5e-05 0 5.4e-05 -0.17 5.8e-05 -0.05
[Var1min, Var1max] = bounds(CH2.Var1) % Check Result
Var1min = -2.0000e-06
Var1max = 2.0000
CH2.Var1 = round(CH2.Var1) % Round To Nearest Integer
CH2 = 500002x2 table
Var1 Var2 ____ _____ 0 -0.18 0 0.02 0 -0.18 0 -0.16 0 -0.27 0 -0.09 0 -0.05 0 -0.19 0 -0.01 0 -0.14 0 -0.25 0 -0.05 0 -0.11 0 0 0 -0.17 0 -0.05
[Var1min, Var1max] = bounds(CH2.Var1) % Check Result
Var1min = 0
Var1max = 2
CH4 = readtable(Uz{1}) % Original
CH4 = 500002x2 table
Var1 Var2 ________ _____ -1 -0.18 -1 0.02 -0.99999 -0.18 -0.99999 -0.16 -0.99999 -0.27 -0.99998 -0.09 -0.99998 -0.05 -0.99997 -0.19 -0.99997 -0.01 -0.99997 -0.14 -0.99996 -0.25 -0.99996 -0.05 -0.99995 -0.11 -0.99995 0 -0.99995 -0.17 -0.99994 -0.05
[Var1min, Var1max] = bounds(CH4.Var1) % Original
Var1min = -1.0000
Var1max = 1.0000
CH4.Var1 = CH4.Var1 + 1 % Add 1
CH4 = 500002x2 table
Var1 Var2 _______ _____ -2e-06 -0.18 2e-06 0.02 6e-06 -0.18 1e-05 -0.16 1.4e-05 -0.27 1.8e-05 -0.09 2.2e-05 -0.05 2.6e-05 -0.19 3e-05 -0.01 3.4e-05 -0.14 3.8e-05 -0.25 4.2e-05 -0.05 4.6e-05 -0.11 5e-05 0 5.4e-05 -0.17 5.8e-05 -0.05
[Var1min, Var1max] = bounds(CH4.Var1) % Check Result
Var1min = -2.0000e-06
Var1max = 2.0000
CH4.Var1 = round(CH4.Var1) % Round To Nearest Integer
CH4 = 500002x2 table
Var1 Var2 ____ _____ 0 -0.18 0 0.02 0 -0.18 0 -0.16 0 -0.27 0 -0.09 0 -0.05 0 -0.19 0 -0.01 0 -0.14 0 -0.25 0 -0.05 0 -0.11 0 0 0 -0.17 0 -0.05
[Var1min, Var1max] = bounds(CH4.Var1) % Check Result
Var1min = 0
Var1max = 2
.
  3 件のコメント
Stephen23
Stephen23 2024 年 10 月 7 日
Star Strider
Star Strider 2024 年 10 月 7 日
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')
Uz = 1x2 cell array
{'ch2.csv'} {'ch4.csv'}
CH4 = readtable(Uz{2}) % Original
CH4 = 500002x2 table
Var1 Var2 ________ _____ -1 -0.42 -1 -0.57 -0.99999 -0.39 -0.99999 -0.52 -0.99999 -0.42 -0.99998 -0.53 -0.99998 -0.33 -0.99997 -0.53 -0.99997 -0.39 -0.99997 -0.52 -0.99996 -0.35 -0.99996 -0.58 -0.99995 -0.48 -0.99995 -0.34 -0.99995 -0.66 -0.99994 -0.55
CH4.Var1 = CH4.Var1 + 1 % Add 1
CH4 = 500002x2 table
Var1 Var2 _______ _____ -2e-06 -0.42 2e-06 -0.57 6e-06 -0.39 1e-05 -0.52 1.4e-05 -0.42 1.8e-05 -0.53 2.2e-05 -0.33 2.6e-05 -0.53 3e-05 -0.39 3.4e-05 -0.52 3.8e-05 -0.35 4.2e-05 -0.58 4.6e-05 -0.48 5e-05 -0.34 5.4e-05 -0.66 5.8e-05 -0.55
CH4.Var1 = round(CH4.Var1) % Round To Nearest Integer
CH4 = 500002x2 table
Var1 Var2 ____ _____ 0 -0.42 0 -0.57 0 -0.39 0 -0.52 0 -0.42 0 -0.53 0 -0.33 0 -0.53 0 -0.39 0 -0.52 0 -0.35 0 -0.58 0 -0.48 0 -0.34 0 -0.66 0 -0.55
writetable(CH4, 'ch4_2.csv') % Write To ‘.csv’ File
which('ch4_2.csv') % Check File Exists
/users/mss.system.4224s/ch4_2.csv
% 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.
.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by