Why does MATLAB change the format of cells?

1 view (last 30 days)
Eric Francois
Eric Francois on 17 Dec 2018
Commented: Guillaume on 18 Dec 2018
Hello,
I'm currently using MATLAB to import, calculate, then export some data from/to an Excel workbook.
I'm using actxGetRunningServer() all along the process.
Nevertheless, when saving the workbook and re-opening it later, many cells are changed in a "custom" format, that then have to be changed back on all sheets...
Does someone now why it occurs? Maybe be before saving and closing the Excel, there would be a finishing call to do to close properly the interaction?
Thank you in advance, and have a nice day,
Eric
  3 Comments
Eric Francois
Eric Francois on 17 Dec 2018
Guillaume,
Thank you for your comment.
I'm externalizing a calculation process from an Excel Workbook, which need to be currently opened to permite users make simulations.
To give you more details on what is the problem, let's resume here my code.
Excel = actxGetRunningServer('Excel.Application'); % it connects matlab to excel
workbooks = Excel.Workbooks;
numWorkbooks = workbooks.Count; % it counts the number of excel files already opened
filename1 = workbooks.Item(numWorkbooks).Name; % it opens the last excel doc in list
workbooks.Item(numWorkbooks).Activate;
Once it is connected, I'm using the modified function "xlsread1()" to import needed data.
A=single(xlsread1(filename,sheet,ExcelZone));
Then MATLAB does its calculations, and returns outputs. The latters are exported back to the excel Workbook with the other modified function "xlswrite1()"
xlswrite1(filename,B,sheet,ExcelZone);
And then, the user is back on Excel, until to save and close it. Reopening it, many cells have changed of format, in a "Custom" one that need to be changed back...
I have found those functions xlsread1() and xlswrite1() in MATLAB's forums (https://es.mathworks.com/matlabcentral/fileexchange/22365-function-for-faster-data-transfer-matlab-excel and https://es.mathworks.com/matlabcentral/fileexchange/10465-xlswrite1), thanks to other users exchange.
Their aim is initially to be as functionnal as native functions, but avoiding to open and close the excel workbook at each call. While in my case, I try to use it the same way, but with actxGetRunningServer().
Before I was using xlswrite() and xlsread() and there wasn't any problem: but since I'm trying to couple the use of actxGetRunningServer(), there is this cells formatting issue...
Thank you in advance for your help,
Sincerely,
Eric
Guillaume
Guillaume on 18 Dec 2018
You've already gone to the trouble of writing your own automation code. I would go the whole way and do the reading and writing myself rather than relying on these xlsread1 and xlswrite1 which frankly are of poor quality (the use of evalin on the the first line, and the fact that the function validates the file input to then never use it are disturbing).
However, I don't think the functions are responsible for your problem. The data insertion is fairly basic and shouldn't affect the formatting. To understand what is happening, it would be useful to have a spreadsheet before the data insertion, the data that you insert (in a mat file) and the range at which you insert it. The incorrect after spreadsheet would be useful as well.
Also, which version of excel are you using?

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!