Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.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
Matlab 将数据集列转换为obsnames名称_Matlab_Dataset - Fatal编程技术网

Matlab 将数据集列转换为obsnames名称

Matlab 将数据集列转换为obsnames名称,matlab,dataset,Matlab,Dataset,我的工作区中有许多大型数据集数组(从.mat文件加载) 一个最简单的工作示例如下 >> disp(old_ds) Date firm1 firm2 firm3 firm4 734692 880,0 102,1 32,7 204,2 734695 880,0 102,0 30,9 196,4 734696 880,0

我的工作区中有许多大型数据集数组(从.mat文件加载)

一个最简单的工作示例如下

>> disp(old_ds)

Date        firm1       firm2       firm3       firm4
734692      880,0       102,1       32,7        204,2
734695      880,0       102,0       30,9        196,4
734696      880,0       100,0       30,9        200,2
734697      880,0       101,4       30,9        200,2
734698      880,0       100,8       30,9        202,2
其中第一行(带字符串)已经是数据集中的标题,也就是说,如果我运行
old_ds.Properties.VarNames
,它们已经显示出来
我想知道是否有一种简单和/或快速的方法可以将第一列命名为ObsNames

作为第一种方法,我考虑“导出”数据矩阵(示例中的第2列到第5列)、日期向量,然后创建一个新的数据集,其中的行具有名称。 即:

除了最后一行返回以下错误之外

Error using setobsnames (line 25)
NEWNAMES must be a nonempty string or a cell array of nonempty strings.

Error in dataset (line 377)
    a = setobsnames(a,obsnamesArg);

Error in mat2dataset (line 75)
    d = dataset(vars{:},args{:});
…我会找到一个解决方案,然后创建一个函数(比如为我拥有的所有22个数据集数组泛化该过程),然后运行该函数22次(每个数据集数组一次)。 为了更好地理解问题,每个数据集有7660行和一些列,范围从2到1320

我不知道如何(如果可以的话)让数据集直接“吃掉”第一列作为ObsNames

谁能给我一个提示吗


编辑:附加一个。

您几乎就要到了,您收到的错误消息基本上是Obsname必须是字符串。在您的例子中,“dates”变量是包含双精度的单元格数组。所以你只需要把它们转换成字符串

mat = double(piHU(:,2:end));         % taking the data, making it a matrix array
head = piHU.Properties.VarNames    % saving headers
head(1) = [];                      % getting rid of 'Date' from head

dates = dataset2cell(piHU(:,1));   % taking dates as column cell array, here dates are of type double. try typing on the command window class(dates{2}), you can see the output is double.
dates(1) = [];  % getting rid of 'Date' from dates
dates_str=cellfun(@(s) num2str(s),dates,'UniformOutput',false); % convert dates to string, now try typing class(dates_str{2}), the output should be char

new_ds = mat2dataset(mat,'VarNames',head,'ObsNames',dates_str); % construct new dataset.

事实上,这应该很容易(但事实上,我正在阅读你的问题,这意味着有同样的问题,我首先在谷歌上搜索它,然后再查找文档…;)

加载数据集时,请使用以下命令(当然要根据您的情况进行调整):

“ReadObsNames”默认值为false。它获取第一列的标题,并将其作为A.Properties.DimNames中第一个维度的名称保存在文件或范围中。 (请参阅“使用文本文件或Excel电子表格作为输入时可用的名称/值对”一节)


我无法下载您的示例文件,但是如果您还没有解决其他问题,请尝试建议的解决方案并告诉它是否有效。很高兴能为您提供帮助。

您能上传一个示例文件吗?@Cici,我已经编辑了我的问题,添加了一个指向示例文件的链接(最后一行)。希望你能帮忙。非常感谢。无法打开您的文件。。。可能在保存时尝试添加-v7.3标记?@Cici,链接已更新,新文件已使用-v7.3标记保存。谢谢你的帮助。谢谢@Cici,但尽管我的问题不是很清楚(我的错),我实际上想问是否存在一种更简单或更快(或两者兼有)的方法来获得相同的结果(如函数)。据我所知,用于导入数据(例如,从csv)的GUI不允许设置ObsNames(运行R2013a),而且我还必须熟悉读取/保存/加载命令。顺便说一句,如果我获得足够的声誉,我会支持您的答案:)我明白了,您可能能够使用set('Obsname',Obsname),将Obsname转换为字符串的单元格数组后。
mat = double(piHU(:,2:end));         % taking the data, making it a matrix array
head = piHU.Properties.VarNames    % saving headers
head(1) = [];                      % getting rid of 'Date' from head

dates = dataset2cell(piHU(:,1));   % taking dates as column cell array, here dates are of type double. try typing on the command window class(dates{2}), you can see the output is double.
dates(1) = [];  % getting rid of 'Date' from dates
dates_str=cellfun(@(s) num2str(s),dates,'UniformOutput',false); % convert dates to string, now try typing class(dates_str{2}), the output should be char

new_ds = mat2dataset(mat,'VarNames',head,'ObsNames',dates_str); % construct new dataset.
cell_dat{1} = dataset('File', 'YourDataFile.csv', 'Delimiter', ';',...
    'ReadObsNames', true);