Retrieve data from a particualr row

5 Ansichten (letzte 30 Tage)
Priyanka L
Priyanka L am 29 Mai 2019
Kommentiert: Priyanka L am 30 Mai 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.

Akzeptierte Antwort

Raj
Raj am 30 Mai 2019
Bearbeitet: Raj am 30 Mai 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 Kommentar
Priyanka L
Priyanka L am 30 Mai 2019
This is working. Thank you for the solution.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by