Retrieve data from a particualr row

7 visualizaciones (últimos 30 días)
Priyanka L
Priyanka L el 29 de Mayo de 2019
Comentada: Priyanka L el 30 de Mayo de 2019
I have attached a 'txt' file. If I open the txt file in Excel then I can see rows with the follwing
'Coal Consumption'
'Oil Consumption'
'Ngas Consumption'
'Biomass Consumption'
'Nuclear Fuel Consumption'
I need to retrieve the values in the adjacent column of 'Coal Consumption' , 'Oil Consumption' etc and export to an excel file. I need the output similar to the outfile_eg.xlsx attached.

Respuesta aceptada

Raj
Raj el 30 de Mayo de 2019
Editada: Raj el 30 de Mayo de 2019
Use this:
%% Initialize variables.
filename = 'C:\Users\Raj\Desktop\out_Denmark2030Alternative.txt'; % Put your text file path here
delimiter = '\t';
startRow = 28;
endRow = 32;
%% Format text:
% column1: text (%s) Consumption Type
% column2: double (%f) Qty
formatSpec = '%s%f%[^\n\r]';
%% Open the text file.
fileID = fopen(filename,'r');
%% Read columns of data according to format string.
Data = textscan(fileID, formatSpec, endRow-startRow+1, 'Delimiter', delimiter, 'HeaderLines', startRow-1, 'ReturnOnError', false);
%% Close the text file.
fclose(fileID);
%% Create output variable
A = table(Data{1:end-1}, 'VariableNames', {'ConsumptionType','Qty'});
A = table2cell(A); % Convert table to cell
A=A.';
%% Clear temporary variables
clearvars filename delimiter startRow endRow formatSpec fileID dataArray ans Data;
%% Write data to Excel file
xlswrite('Data.xls',A)
  1 comentario
Priyanka L
Priyanka L el 30 de Mayo de 2019
This is working. Thank you for the solution.

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by