使用 MATLAB 通过 ActiveX 协议将字符串值写入 Excel 电子表格.. 发现问题
Using MATLAB to write String values into Excel spreadsheet via ActiveX protocol.. found a problem
我使用的是 MATLAB 2017a,过去一直使用 xlswrite
来执行此操作。我 运行 遇到的问题是执行速度,我正在寻找更好的方法。因此,我决定使用 actxserver
并使用来自 MATLAB 的 get(obj)
和来自 ActiveX 的 Range.Value
写入数据。代码如下所示:
e = actxserver('Excel.Application);
eWorkbook = e.Workbooks.Add;
e.Visible = 1;
eSheets = e.ActiveWorkbook.Sheets;
eSheet1 = eSheets.get('Item',1);
eSheet1.Activate;
A = ["Str1";"Str2";"Str3";];
eActivesheetRange = get(e.Activesheet, 'Range', 'A1:A3');
eActivesheetRange.Value = A;
这段无伤大雅的代码没有执行,也没有抛出警告或错误消息。没什么。在我看来,eActivesheetRange
在 ActiveX 端的计算结果为:Range("A1:A3")
。有趣的是,如果我替换
A = ["Str1";"Str2";"Str3";];
和
A = char(["Str1";"Str2";"Str3";]);
然后程序将 A 字符数组写入 eActivesheetRange
范围内的每个单元格。
有没有办法使用 MATLAB Range.Value
连接来调用 cells()
? cells().Value
能解决这个问题吗?
解决这个问题的方法当然是 for
循环。
alphacolumn=char(97:117);
% iterate through data array
for i=1:21
str=string(alphacolumn(i))+2;
str2=string(alphacolumn(i))+202;
write1=char(str+":"+str2);
if ~isreal(tsc{i,1})
T = (tsc{i,1});
for j = 1:length(T)
rrange = xl.ActiveWorkbook.Activesheet.Range(char(string(alphacolumn(i)) + string(j+1)));
xlcompatiblestring1 = char(string(T(j,:,:)));
rrange.Value= xlcompatiblestring1;
end
else
tsci=tsc{i,1};
% write data to xl target file
%xlswrite(xlfilepath,tsci,write1);
xlActivesheetRange = get(xl.Activesheet,'Range',write1);
xlActivesheetRange.Value = tsci;
end
end
我不认为 writing to Excel using ActiveX is able to handle string
types properly. In this case, you can make it work by simply converting your string array into a cell array of character vectors using cellstr
。将最后一行代码更改为以下对我有效(在 R2016b 中):
eActivesheetRange.Value = cellstr(A);
用以下内容替换最后两行也有效:
e.Activesheet.Range('A1:A3').Value = cellstr(A);
我使用的是 MATLAB 2017a,过去一直使用 xlswrite
来执行此操作。我 运行 遇到的问题是执行速度,我正在寻找更好的方法。因此,我决定使用 actxserver
并使用来自 MATLAB 的 get(obj)
和来自 ActiveX 的 Range.Value
写入数据。代码如下所示:
e = actxserver('Excel.Application);
eWorkbook = e.Workbooks.Add;
e.Visible = 1;
eSheets = e.ActiveWorkbook.Sheets;
eSheet1 = eSheets.get('Item',1);
eSheet1.Activate;
A = ["Str1";"Str2";"Str3";];
eActivesheetRange = get(e.Activesheet, 'Range', 'A1:A3');
eActivesheetRange.Value = A;
这段无伤大雅的代码没有执行,也没有抛出警告或错误消息。没什么。在我看来,eActivesheetRange
在 ActiveX 端的计算结果为:Range("A1:A3")
。有趣的是,如果我替换
A = ["Str1";"Str2";"Str3";];
和
A = char(["Str1";"Str2";"Str3";]);
然后程序将 A 字符数组写入 eActivesheetRange
范围内的每个单元格。
有没有办法使用 MATLAB Range.Value
连接来调用 cells()
? cells().Value
能解决这个问题吗?
解决这个问题的方法当然是 for
循环。
alphacolumn=char(97:117);
% iterate through data array
for i=1:21
str=string(alphacolumn(i))+2;
str2=string(alphacolumn(i))+202;
write1=char(str+":"+str2);
if ~isreal(tsc{i,1})
T = (tsc{i,1});
for j = 1:length(T)
rrange = xl.ActiveWorkbook.Activesheet.Range(char(string(alphacolumn(i)) + string(j+1)));
xlcompatiblestring1 = char(string(T(j,:,:)));
rrange.Value= xlcompatiblestring1;
end
else
tsci=tsc{i,1};
% write data to xl target file
%xlswrite(xlfilepath,tsci,write1);
xlActivesheetRange = get(xl.Activesheet,'Range',write1);
xlActivesheetRange.Value = tsci;
end
end
我不认为 writing to Excel using ActiveX is able to handle string
types properly. In this case, you can make it work by simply converting your string array into a cell array of character vectors using cellstr
。将最后一行代码更改为以下对我有效(在 R2016b 中):
eActivesheetRange.Value = cellstr(A);
用以下内容替换最后两行也有效:
e.Activesheet.Range('A1:A3').Value = cellstr(A);