使用" xlswrite" MATLABs含有不同大小的字符串单元阵列 [英] Using "xlswrite" MATLABs for cell arrays containing strings of different size

查看:972
本文介绍了使用" xlswrite" MATLABs含有不同大小的字符串单元阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用xlswrite一个单元阵列4X10左右,在每个元素不同长度的字符串,返回MATLAB这个错误:

When I tried using xlswrite for a cell array 4X10 or so with strings of different lengths in each element, MATLAB returned this error:

Error using xlswrite (line 188)
An error occurred on data export in CSV format.

Caused by:
    Error using dlmwrite (line 118)
    The input cell array cannot be converted to a matrix.

我煮沸下来的是,在由xlswrite称为dlmwrite功能某处,它调用cell2mat,这将连接我的单元阵列的元素的字符阵列。然而,将垂直和水平方向并置的元件,它是不可能来连接字符部件垂直如果它们不具有相同的长度。你最终会与不一致的尺寸阵列。例如,

What I boiled it down to is that somewhere in the "dlmwrite" function that is called by "xlswrite", it calls "cell2mat" which will concatenate the elements of my cell array to a character array. However, it will concatenate the elements vertically and horizontally, and it is not possible to concatenate character elements vertically if they do not have the same length. You will end up with an array on inconsistent dimensions. For example,

如果我有ABCDEF和ABC,他们连接起来垂直会给:

If I have 'abcdef' and 'abc', concatenating them vertically would give:

ABCDEF

ABC

的第一行的6的长度,并且所述第二行具有3的长度,如果你在谈论的矩阵,这应该是2X6在这种情况下,不使逻辑意义

The first row has a length of 6, and the second row has a length of 3, which does not make logical sense if you're talking about a matrix, which should be 2X6 in this case.

有谁知道这个解决方法?我与这个故障在MATLAB受挫pretty。

Does anybody know a workaround for this? I'm pretty frustrated with this glitch in MATLAB.

推荐答案

我不明白你的错误 - 至少在我的版本的投入。有一个在您的一个错误,但我想这只是在注释,而不是在您的实际code。

I do not get your error - at least with my version of the input. There is an error in your 'a', but I guess that is just in the comment and not in your actual code.

也许错误得到解决吗?我使用2012A。

Maybe the error got fixed? I am using 2012a.

不管怎样;您可以使用建议的解决方法。这是code的只有一行。

Anyway; you can use the suggested workaround. It is just a single line of code.

% here are your data
a = {'$','gsqtmpiv','lsso';...
    'gsqqmwwmsr','efwxvegxmsr','gpsgo';...
    'hexyq','tst','vyffiv';...
    'pek','geqive','xerkmivw';...
    'tvigsppyhih','fewmexih','vywxmge'};

% this works fine in 2012a
xlswrite('blah.xls',b)

% but using dlmwrite directly fails
% like you described
try
    dlmwrite('test.txt', a)
catch ME
    disp(ME.message)
end

% so instead use the suggested work-around:
b = cellfun(@(s) sprintf('%-12s', s), a, 'UniformOutput', false);
% and dlmwrite will no longer fail
dlmwrite('test.txt', b)

您需要知道(在TESTDATA 11)字符串的最大长度和使用,在格式化字符串

You need to know the max length of string (11 in your testdata) and use that in the formatting string

'%12s' 

12给人的最大长度。

gives a max length of 12.

这样做的问题是,如果你有xlswrite使用它,你将在字符串的空间。他们是看不见的,如果你使用

The problem with that is that if you use it with xlswrite, you will have spaces in the strings. They are invisible if you use the

'%-12s' 

格式,所以除非你使用作进一步处理字符串可能没事吧?

formatting, so unless you use the strings for further processing it may be ok?

这篇关于使用" xlswrite" MATLABs含有不同大小的字符串单元阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆