Portfolio Optimization with Matlab and VBA. cell2mat doesn't work -


i trying write macro in vba execute matlab. when opened, matlab uses data stored in excel sheet macro recorded. in vba code recall matlab function , use cell2mat convert data excel matlab. make life easier post code:

public sub usa() dim matlab object set matlab = createobject("matlab.desktop.application") matlab.execute ("cd('c:\users\dales\dropbox\esempi')") serie = sheets("sp500").range("b3:ru686") grupposector = sheets("gruppot").range("b3:ru21") gruppomin = sheets("gruppo").range("z11:z29") gruppomax = sheets("gruppo").range("aa11:aa29") ris = matlab.putworkspacedata("prices", "base", serie) ris = matlab.putworkspacedata("grupposector", "base", grupposector) ris = matlab.putworkspacedata("gruppomin", "base", gruppomin) ris = matlab.putworkspacedata("gruppomax", "base", gruppomax) myresult = matlab.execute("[portrisk, portreturn, portwts] = obama( cell2mat(prices, cell2mat(grupposector), cell2mat(gruppomin), cell2mat(gruppomax));")  ris = matlab.getworkspacedata("portrisk", "base", portrisk) ris = matlab.getworkspacedata("portreturn", "base", portreturn) ris = matlab.getworkspacedata("portwts", "base", portwts)  sheets("spottimizzazione").range("a3:a18") = portrisk sheets("spottimizzazione").range("b3:b18") = portreturn sheets("spottimizzazione").range("e3:rx18") = portwts end sub 

now when matlab opened macro, doesn't convert inputs in double format, stored strings.

where wrong?

your problem that, said, data imported strings. calling cell2mat directly not work, because keep data stored char.

what need do, in order solve problem, convert every cell in cell array doubles before transforming cell matrix.

this can achieved calling :

 myresult = matlab.execute("[portrisk, portreturn, portwts] = obama(...  cell2mat(cellfun(@str2double,prices,'uniformoutput',false)),...   cell2mat(cellfun(@str2double,grupposector,'uniformoutput',false)),...   cell2mat(cellfun(@str2double,gruppomin,'uniformoutput',false)),...   cell2mat(cellfun(@str2double,gruppomax,'uniformoutput',false)));") 

this work, of course, if obama function needs double matrices inputs.


Comments

Popular posts from this blog

html - Difficulties with background-image property -

visual studio code - What does the isShellCommand property actually do and how should you use it? -

ios - Segue not passing data between ViewControllers -