How can I combine charactor array with a numerical arrays?

6 visualizzazioni (ultimi 30 giorni)
I have more than 1000 data sets like the attached file, just need to extract all the details except GG and NN. I jus use the following programme to import from text file and extract the details. but I can not combine the character array and numeric array after running the programme.
filename = 'L:\R1.txt';
delimiter = {',','-'};
formatSpec = '%s%s%s%s%s%*s%*s%s%[^\n\r]';
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'ReturnOnError', false);
fclose(fileID);
raw = repmat({''},length(dataArray{1}),length(dataArray)-1);
for col=1:length(dataArray)-1
raw(1:length(dataArray{col}),col) = dataArray{col};
end
numericData = NaN(size(dataArray{1},1),size(dataArray,2));
for col=[2,3,4,5]
rawData = dataArray{col};
for row=1:size(rawData, 1);
regexstr = '(?<prefix>.*?)(?<numbers>([-]*(\d+[\,]*)+[\.]{0,1}\d*[eEdD]{0,1}[-+]*\d*[i]{0,1})|([-]*(\d+[\,]*)*[\.]{1,1}\d+[eEdD]{0,1}[-+]*\d*[i]{0,1}))(?<suffix>.*)';
try
result = regexp(rawData{row}, regexstr, 'names');
numbers = result.numbers;
invalidThousandsSeparator = false;
if any(numbers==',');
thousandsRegExp = '^\d+?(\,\d{3})*\.{0,1}\d*$';
if isempty(regexp(thousandsRegExp, ',', 'once'));
numbers = NaN;
invalidThousandsSeparator = true;
end
end
if ~invalidThousandsSeparator;
numbers = textscan(strrep(numbers, ',', ''), '%f');
numericData(row, col) = numbers{1};
raw{row, col} = numbers{1};
end
catch me
end
end
end
rawNumericColumns = raw(:, [2,3,4,5]);
rawCellColumns = raw(:, 1);
v = rawCellColumns(:, 1);
D1 = cell2mat(rawNumericColumns(:, 1));
D2 = cell2mat(rawNumericColumns(:, 2));
D3 = cell2mat(rawNumericColumns(:, 3));
t = cell2mat(rawNumericColumns(:, 5));
but then I can not use the disp([v D1 D2 D3 t]). if I use it there is an error. please kindly help me to solve this problem
  2 Commenti
Stephen23
Stephen23 il 14 Mar 2016
Modificato: Stephen23 il 14 Mar 2016
" I can not combine the character array and numeric array"
This is correct: numeric arrays are numeric arrays, and character arrays are character arrays. They are different things. They don't just "combine" into something that you can display (see ##).
If you are interested in displaying numbers and characters then you should just use fprintf.
## Of course it is easy to convert a character array to its equivalent character values, which are numeric, but these are not usually what people wish to display...
Nadeera Gunartna
Nadeera Gunartna il 15 Mar 2016
when i use this "fprintf('%.2f %.2f\n', [v D1].')" display this error
"Error using horzcat Dimensions of matrices being concatenated are not consistent.

Accedi per commentare.

Risposte (1)

Eric Lowry
Eric Lowry il 16 Mar 2016
You are seeing an error when you run your disp command because you are concatenating v, which is a cell array, with D1, D2, D3, and t, which are matrices. You could use something like this, instead:
disp([v num2cell(D1) num2cell(D2) num2cell(D3) num2cell(t)]);
This will convert the matrices into cell arrays and concatenate the results.

Community Treasure Hunt

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

Start Hunting!

Translated by