在Matlab中将字符串(导入文件的名称)包含在数值数组中

时间:2015-07-06 22:19:07

标签: arrays string matlab

我在我的脚本(Matlab)中创建一个数组,收集我从数据文件中计算出的所有结果。现在,如果我能够在这个数组中包含文件名,这将是很好的,以便在以后的使用中轻松识别。

我现在得到的是(非常简化 - 无法运行):

%Importing file:
FileName=uigetfile('*.CSV', 'Select .CSV-file','MultiSelect', 'off');
%Doing come calc on the data from the file imported...
%Results:
A=12;
B=4;
C=17;
D=124;
%Combining in array:
results=[A; B; C; D]

这很好用。 现在,我想在这个数组中包含FileName:

results=[FileName; A; B; C; D]

这给了我错误:

Error using vertcat Dimensions of matrices being concatenated are not consistent.

关于如何解决这个问题的任何想法?我对此非常新手。我知道这与文件名是一个字符串有关(我已经尝试了cellstr(FileName),但它没有帮助。另外,我只试过'FileName') 。

1 个答案:

答案 0 :(得分:0)

您不能将任意字符串添加到特定大小的矩阵中,这是在"容器中混合两种类型的数据"这对他们两个都不合适。要解决该问题,您应该将其保存为struct数组或单元格数组,以及Matlab如何处理这些情况,例如:

results.name='Filename';
results.data=[A; B; C; D];

所以你有一个变量results,可以使用它的字段名results.name等来访问其中不同类型的数据....

编辑:

为了将此信息移动到excel文件,最好使用单元格数组:

your_xls_filename = 'bla.xlsx'; %$ just for the example
results = {'FileName'; A ; B ; C; D}; % this is a cell array
sheet = 1; % just for the example
xlRange = 'E1'; % just for the example
xlswrite(your_xls_filename,results,sheet,xlRange);

现在打开bla.xlsx,看看你得到了什么。

相关问题