MATLAB 将 Cell 数组数据合并为一行字符串
可以使用 cellfun 函数和 strjoin 函数实现将 Cell 数据变成一行字符串。
示例代码:
% 创建一个 Cell 数组
cell_data = {'hello', 'world', '!', 123};
% 使用 cellfun 函数将 Cell 数组中的所有元素转换为字符串
str_data = cellfun(@num2str, cell_data, 'UniformOutput', false);
% 将所有字符串合并为一个字符串,中间用空格隔开
str = strjoin(str_data, ' ');
% 输出结果
disp(str); % hello world ! 123
解释:
首先使用 cellfun 函数将 Cell 数组中的所有元素转换为字符串,这里使用了 num2str 函数将数字转换为字符串。将 'UniformOutput' 参数设为 false,表示输出的是一个 Cell 数组。
然后使用 strjoin 函数将所有字符串合并为一个字符串,中间用空格隔开。
最后输出结果即可。
原文地址: https://www.cveoy.top/t/topic/ngGl 著作权归作者所有。请勿转载和采集!