Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 将字符串的单元格数组放入矩阵或表格中_String_Matlab_Concatenation_Cell Array - Fatal编程技术网

String 将字符串的单元格数组放入矩阵或表格中

String 将字符串的单元格数组放入矩阵或表格中,string,matlab,concatenation,cell-array,String,Matlab,Concatenation,Cell Array,我从对一些数据执行的计算中收集信息,并存储到数组中。我还有一些关于这些数据的信息,这些信息来自一个文本文件,该文件有时包含字符串 文本文件中的字符串被保存到字符串的{}单元格数组中,例如: strings={'s1' 's2' 's3'}; a=[1 2 3] 字符串和数组所包含的内容是根据文本文件中的数据以及我在matlab中通过循环得到的一些数据中的一些条件生成的,循环如下: srings{e}=blablahFromSomewhere{e} a(e)=otherNumericalBlah

我从对一些数据执行的计算中收集信息,并存储到数组中。我还有一些关于这些数据的信息,这些信息来自一个文本文件,该文件有时包含字符串

文本文件中的字符串被保存到字符串的
{}
单元格数组中,例如:

strings={'s1' 's2' 's3'};
a=[1 2 3]
字符串和数组所包含的内容是根据文本文件中的数据以及我在matlab中通过循环得到的一些数据中的一些条件生成的,循环如下:

srings{e}=blablahFromSomewhere{e}
a(e)=otherNumericalBlahBlahFromSomwehre(e+6)
最后,我想把它合并成一张表。我通常会这样做:

T=[a(:) strings(:)]
但我面临以下错误:

Error using horzcat
Dimensions of matrices being concatenated are not consistent.
有人能帮忙吗?我真的不想将字符串转换成整数,因为在运行分析时,字符串的内容在输出中更方便


谢谢:)

如果您想获得字符数组:

    aux = num2str(a(:));
    aux = mat2cell(aux,ones(1,size(aux,1)),size(aux,2));
    T = cell2mat([aux strings(:)]);
结果是一个2D字符数组(如果需要,将引入前导空格):


有点不清楚你想要什么,但似乎是:

T = table(a(:), strings(:));
假设我正确阅读了
表的文档

或者,对于单元阵列:

C = [num2cell(a(:)) strings(:)];
代码

strings={'s1' 's2' 's3'};
a=[1 2 3];
outputfile = 'output.txt';

%%// Code to horziontally concatenate to result in a Nx2 cell array
out = [num2cell(num2str(a,'%d')') strings']

%%// Write to outputfile - Method 1
out = out';
fid = fopen(outputfile,'w');
fprintf(fid, '%s\t%s\n', out{:});
fclose(fid);

%%// Write to outputfile - Method 2
%%// Create a text file and clear it out of any content. This is needed, as otherwise
%%// XLSREAD was initializing CSV files with weird characters
%% dlmwrite(outputfile,'');

%%// Write to CSV file using XLSREAD
%xlswrite(outputfile,out)

%%// Verify
type(outputfile)
输出

out = 

    '1'    's1'
    '2'    's2'
    '3'    's3'


1   s1
2   s2
3   s3

OP提到了“T=[a(:)strings(:)]”,所以我想他需要一个单元格输出。嘿@Luis,谢谢。这不是我想要的。最终表格应包括两列,一列为
a
,另一列为
字符串
@Bastien So。。。你想要一个2D单元格数组作为结果吗?我想2D单元格数组也可以。虽然dlmwrite可能不喜欢它(请参阅下面的回答),但我认为这将是完美的,但由于我使用dlmwrite写入文本文件,我得到以下结果:
使用dlmwrite时出错(第118行)无法将输入单元格数组转换为矩阵。
@Bastien我从未使用过它,但根据文档
dlmwrite
仅处理数字数据,而不是字符串。如果您有足够新的Matlab或stats工具箱,那么使用表和
writetable
看起来可能会奏效。遗憾的是我没有写字台。。。我只是一个版本下有一个sadfaceOk这是非常接近,但当我使用以下
dlmwrite(文件名,T,'-append',分隔符','\T')字符串被拆分为与选项卡一样多的“列”。根据您的新要求进行编辑!谢谢你的回复。它给了我每个用逗号分隔的字符。我还收到一个错误,它无法启动Excel。这是一项令人头痛的任务。似乎
xlswrite
是PC独有的东西。下载了由第三方开发的mac的“修复程序”,但我只会保存在xls中,因为这个表格必须转到R。该死的MATLAB!伟大的棒 极 了非常感谢你的帮助和耐心!
out = 

    '1'    's1'
    '2'    's2'
    '3'    's3'


1   s1
2   s2
3   s3