calculation in csv through matlab
显示 更早的评论
we know like xcell operations can be performed in csv like multipling 2 rows etc
eg) when we append data to csv from a matrix , we get a row of data
suppose i want that data*2 but not perform that calculation in matlab and do it in csv (=A1*2 %this type)
and then read the next coloumn (B) to get the result
how do i proceed?
thaks for any help you can provide
4 个评论
Cris LaPierre
2021-3-21
A csv file is not an Excel file. It usually refers to a specific format of a delimited text file.
You can't add equations to a csv file. Once that is done, the file can only be saved in an Excel file type (xlsx, xls, etc).
If you don't want to use MATLAB for your data processing, then why not just add the equation yourself in Excel?
dpb
2021-3-21
You can use writecell to enter formulas into Excel, but I've found it extremely slow and will, in fact, hang/crash MATLAB to try to do so to write many individual cells scattered around the spreadsheet. It will work to do one cell or row or column or other range, but as Chris says, it has to be in a spreadsheet, not a text file type.
SINDU GOKULAPATI
2021-3-22
Cris LaPierre
2021-3-22
Sure you can add them. You just can't save the equations in a csv file. If you save as a csv file, the equations are replaced with values. Close and reopen the csv file, and you'll see that the updated values are still there, but all the equations are gone.
回答(1 个)
Cris LaPierre
2021-3-22
编辑:Cris LaPierre
2021-3-22
You may find the following two posts helpful.
- https://www.mathworks.com/matlabcentral/answers/47838-how-to-apply-a-formula-autofill-on-a-range-of-cells-in-excel-using-matlab-com-activex#answer_58688
- https://www.mathworks.com/matlabcentral/answers/761711-readtable-excel-date-base#comment_1366841
The resulting code would be something like this.
file = 'D:\Applications\work\example.csv'; % This must be full path name
% Create connection to Excel
excelapp = actxserver('Excel.Application');
wkbk = excelapp.Workbooks.Open(file);
% Write equation to specified cell:
EqnRange = wkbk.Activesheet.get('Range','B1');
EqnRange.Formula = '=A1*2';
% Save changes. This leaves the value in the csv file, not the equation
invoke(wkbk,'Save');
% Close Excel and clean up
invoke(excelapp,'Quit');
delete(excelapp);
clear excelapp;
类别
在 帮助中心 和 File Exchange 中查找有关 Spreadsheets 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!