Error with cell2mat

3 次查看(过去 30 天)
Sandro Bianchini
Sandro Bianchini 2016-6-16
Hello,
I receive the following error from Matlab when using the cell2mat function: "Cell contents reference from a non-cell array object. cellclass = class(c{1});"
The code works with excel, which give a matrix to Matlab. When the numeric matrix is imported by Matlab it is in the cell format. Then I use the cell2mat function to convert it in double.
The line of the error is the following:
Data=cell2mat(price);
If I use the same line in the command window (with the same numeric matrix imported from excel) it works.
I don't understand where is the mistake.
Thanks

回答(1 个)

Star Strider
Star Strider 2016-6-16
‘I don't understand where is the mistake.’
We don’t either, because we don’t have your code or your Excel file. Having those to refer to and experiment with would help significantly.
  2 个评论
Sandro Bianchini
Sandro Bianchini 2016-6-20
I am sorry I haven't given enough details. The code is the following:
Sheets("Input Data").Select
Range("price_data").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Price = Selection
Dim MatLab As Object
'Set MatLab = CreateObject("Matlab.Application")
Set MatLab = CreateObject("MatLab.Desktop.Application")
cartella_matlab = Sheets("Link").Range("cartella_matlab").Value
MatLab.Execute ("cd('" & cartella_matlab & "')")
'MatLab.Execute ("cd('C:\matlab folder path')")
ris = MatLab.PutWorkspaceData("price", "base", Price);
This is how I pass the numbers to Matlab. Then the macro executes the Matlab code with the following function:
myResult = MatLab.Execute("[PortRisk, PortReturn, PortWts, vola_att, rend_medi, covarianza_hist]=Model_AAA_new(cell2mat(price));")
As you can see I have put a cell2mat inside the input brackets. It doesn't work because if I enter into Matlab I can see that the variable "price" is still cell. But the code goes on till the end even if I know it works wrongly because I get unreasonable results at the end ( negative variance for example).
If I put inside the Matlab function the line
price=cell2mat(price);
to convert price to double, it gives me the error "Cell contents reference from a non-cell array object. cellclass = class(c{1});".
But if I stop the execution of the code and I put manually the same line "price=cell2mat(price);" in the command window, it works just fine and I can get to the end of the code with the right results.
In the excel sheet the variable "price" is a range of numbers and, as you know better than me, they are in the format with the " , " as separator; instead Matlab use "." as separator for decimals.
At the beginning of the code in VBA I use the following line to convert the format with "." as separator:
Application.UseSystemSeparators = True
I really need to have all the variables in Matlab as double, for now they enter as cell.
Thanks in advance for your help.
Star Strider
Star Strider 2016-6-20
I cannot give you an exact response because I do not have whaterver ‘price’ is. The error leads me to believe that ‘price’ is not a cell array.
See if just referring to ‘price’ rather than ‘cell2mat(price)’ does what you want.
If not, you may have to experiment with other ways of converting it to a double array, depending on the class of variable it is.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by