How can I combine charactor array with a numerical arrays?

3 visualizaciones (últimos 30 días)
Nadeera Gunartna
Nadeera Gunartna el 14 de Mzo. de 2016
Comentada: Nadeera Gunartna el 24 de Mzo. de 2016
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 comentarios
Stephen23
Stephen23 el 14 de Mzo. de 2016
Editada: Stephen23 el 14 de Mzo. de 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 el 15 de Mzo. de 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.

Iniciar sesión para comentar.

Respuestas (1)

Eric Lowry
Eric Lowry el 16 de Mzo. de 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.

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by