I want to import the data from excel sheet ensuring that the logic behind the sheet is not lost. So the formula remains intact. What is the procedure for that?
File attached.

 채택된 답변

Eric
Eric 2014년 5월 15일

0 개 추천

Here's a snippet of code to get you started. I created a spreadsheet called delme.xlsx. In cell A1 I put the value 1. In cell A2 I used the formula "=PI()". I named cell A1 "Value1" and cell A2 "Value2". In cell A3 I used the formula "=sum(Value1,Value2)".
fname = 'c:\temp\delme.xlsx';
xlObj = actxserver('Excel.Application');%Start Excel
xlObj.Visible = 1;%Make Excel visible
wsObj = xlObj.Workbooks.Open(fname);%Open workbook
Sheet = wsObj.Sheets.Item(1);%Assume we're using the first sheet
A3_value = Sheet.Range('A3').Value
A3_formula = Sheet.Range('A3').Formula
A3_value is equal to 4.141592653589793 and A3_formula is the string "=SUM(Value1,Value2)".
Hopefully this helps,
Eric

댓글 수: 4

Eric
Eric 2014년 5월 15일
By the way, I used named ranges in Excel as I think that will make your job of maintaining traceability much easier. If you do not use named ranges in your formulas you'll get an answer like "=SUM(A1,A2)" which isn't terribly descriptive.
-Eric
Mamad Mamadi
Mamad Mamadi 2019년 11월 26일
hi Eric i have a question about writing some equation in matlab would you mind if you help me?
Image Analyst
Image Analyst 2019년 11월 26일
Eric hasn't been here in quite some time. If you're willing to accept help from others, I suggest you post your question as a new thread in answers after you read this.
I've not tried but I'd think simply
A3_formula = '=SUM(A1,A2)';
Sheet.Range('A3').Formula=A3_formula;
should do the trick.
Salt to suit, of course...

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Image Analyst
Image Analyst 2019년 11월 26일

1 개 추천

Try this well commented, fully functioning demo I made up. Change the name of the workbook to whatever you're using instead of "Example.xlsx".
% Launch an Excel server using ActiveX (Windows ONLY).
excelObject = actxserver('Excel.Application');
% Create the filename of the existing workbook.
fullFileName = fullfile(pwd, 'Example.xlsx');
% Make sure the file exists.
if ~isfile(fullFileName)
errorMessage = sprintf('The workbook file does not exist:\n%s', fullFileName);
uiwait(errordlg(errorMessage));
return;
end
% Open the workbook from disk.
excelWorkbook = excelObject.workbooks.Open(fullFileName);
% Excel is invisible so far. Make it visible.
excelObject.Visible = true;
% Create a string with the formula just like you'd have it in Excel.
yourFormula = '=SUM(A1..A100)'; % No spaces allowed.
% Assign the formula to the cell "B1".
excelWorkbook.ActiveSheet.Range('B1').Formula = yourFormula;
% Save the current state of the workbook.
excelWorkbook.Save;
% Close the workbook. Excel will stay open but be hidden.
% You can still see it as "Microsoft Excel" in Task Manager.
excelWorkbook.Close;
% Shut down the Excel server instance.
excelObject.Quit;
% Even after quitting, you can still see it as "Microsoft Excel" in Task Manager.
% Clear the excel object variable from MATLAB's memory.
clear('excelObject', 'excelWorkbook', 'yourFormula');
% The clear finally shuts down the server and it no longer appears in Task Manager.
fprintf('Done interacting with Excel.\n');
Image Analyst
Image Analyst 2014년 5월 15일

0 개 추천

I'm sure you can do it using ActiveX, though I'm not sure which ActiveX command is the correct one to use. You can pretty much do absolutely anything with Office apps using ActiveX. It's the figuring out which method to use that is the hard part since there are thousands of methods.

댓글 수: 1

dpb
dpb 2014년 5월 15일
Yeah, should've added the caveat to "can't" -- I was limiting to the "import" idea using a higher-level function.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Data Import from MATLAB에 대해 자세히 알아보기

질문:

2014년 5월 14일

답변:

2019년 11월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by