how to solve an error ?

1 回表示 (過去 30 日間)
pruth
pruth 2018 年 5 月 15 日
編集済み: pruth 2019 年 9 月 25 日
hi, I have written a code for text input files. everything is working fine except last 3 lines of code!!! i am getting this error
///////////////////////// Error using cat Dimensions of matrices being concatenated are not consistent.
Error in cell2mat (line 83) m{n} = cat(1,c{:,n}); ////////////////////////////////////////////
what I just want to do is to get/convert the data from cell 'alldata' to simple array 'rawdata' using cell2mat I tried so much I don't know what is the problem. I have attached the files also. please help. so much frustrated.
% read all text file from folder
clear all
close all
clc
initialdir = cd();
set(0, 'DefaultAxesFontSize',15)
list = dir('*.txt');
delimiter = '\t';
formatSpec = '%s%s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%s%s%s%[^\n\r]';
alldata = [];
for file = 1:length(list)
fid = fopen(list(file).name);
dataArray = textscan(fid, formatSpec, 'Delimiter', delimiter, 'ReturnOnError', false);
%%Close the text file.
fclose(fid);
%%Convert the contents of columns containing numeric strings to numbers.
raw = repmat({''},length(dataArray{1}),length(dataArray)-1);
for col=1:length(dataArray)-1
raw(1:length(dataArray{col}),col) = dataArray{col};
end
raw(1,:) = [];
alldata = [alldata;raw];
end
rawdata(:,1) = datenum((alldata(:,1)),'yyyy-mm-dd') + datenum(alldata(:,2),'HH:MM') - datenum('00:00','HH:MM'); rawdata(:,2) = str2num(cell2mat(alldata(:,3)));
rawdata(:,3) = str2num(cell2mat(alldata(:,4)));
rawdata(:,4) = str2num(cell2mat(alldata(:,5)));

採用された回答

pruth
pruth 2018 年 5 月 15 日
fixed it
rawdata(:,2) = str2double(alldata(:,3));
rawdata(:,3) = str2double(alldata(:,4));
rawdata(:,4) = str2double(alldata(:,5));

その他の回答 (1 件)

Jan
Jan 2018 年 5 月 15 日
If you post the complete error message, the readers can know, which line is failing. I guess it is:
rawdata(:,2) = str2num(cell2mat(alldata(:,3)));
You have padded the raw data with empty string ''. Then cell2mat tries to create a CHAR matrix, but the rows or columns have inconsistent lengths. One solution can be to apply str2num directly on the cell array. Or better use the safer str2double. In addition you might want to pad the data with '0'.
Is creating a matrix from the single columns of dataArray really useful, if all you want to do is to split it up to columns afterwards again?
  1 件のコメント
pruth
pruth 2018 年 5 月 15 日
this is the error.
Error using cat Dimensions of matrices being concatenated are not consistent.
Error in cell2mat (line 83) m{n} = cat(1,c{:,n});
Error in readtxtdistro (line 36) rawdata(:,2) = str2num(cell2mat(alldata(:,3)));

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeText Files についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by